Apr 02

Not all methods are owned by class Thread. Therefore here a small overview which method belongs to which class:

Class Thread:
start();
yield();
sleep();
join();

Class Object:
wait();
notify();
notifyAll();

Interface Runnable:
run();

Tagged with:
Apr 01

Today I would like to go a little bit deeper into interaction-method, e.g. instead of method

wait();

it is also possible to wait only a specified time (in ms) with

wait(ms);

A special notification is not neccessary.

Furthermore instead of just notifiying the next waiting thread with

notify();

also all waiting threads could be woken up and started with

notifyAll();

Tagged with:
Mar 31

As already discussed there is a risk of Threads running into deadlocks, but as we say in German “gefahr gekannt, gefahr gebannt” which means something like “as soon as we know the risk we can do something against it; we can let them interact with each other;

class Shop implements Runnable {

Shop a = new Shop();// creating a common instance which can be accessed from everywhere

public void run () {

if (Thread.currentThread().getName().equals(“Mary”))

sales_flow(); // Mary is locking method sales_flow

else

check_stock(); // Steve is locking method check_stock

}

synchronized void sales_flow(){

if (Thread.currentThread().getName().equals(“Mary”))

check_stock(); // Mary is trying to access method check_stock which is already locked by Steve

}

synchronized void check_stock (){

a.notify(); // The next waiting Thread is informed that it can proceed although the method is not finished completely

if (Thread.currentThread().getName().equals(“Steve”))

sales_flow(); // Steve is trying to access method sales_flow which is already locked by Mary

}

public static void main (String args []){

Shop a = new Shop(); // This needs to be removed
Thread sale1 = new Thread(a);
sale1.setName(“Mary”);
sale1.start();

Thread sale2 = new Thread(a);
sale2.setName(“Steve”);
sale2.start();

synchronized (a){

try {

a.wait(); // The object is synchronized and waiting for other threads to finish (only in case there is another thread started before and running)

} catch (InterruptedException e ){ e.printStackTrace(); }

}

}

}

Tagged with:
Mar 29

As already discussed, a class can not be synchronized on its own, but we are speaking about thread-safe classes in case the methods inside the class are carefully synchronized in order to protect the data.

A good example for (non-)Thread-safe classes are StringBuilder and StringBuffer.  While StringBuffer is Thread-safe StringBuilder is not. Since StringBuilder is not taking care of synchronizing, it is faster than StringBuffer who needs time to take care.

Tagged with:
Mar 28

Usually Threads are synchronized in order to prevent methods from interrupting each other and to mess up data. Please keep in this context in mind that static and non-static methods are never blocking each other as discussed the post before.

In order to change a static variable you should use a static method to change it and to change a non-static variable a non-static method should be used and synchronized. Otherwise things can mess up quite quickly.

Tagged with:
Mar 27

static synchronized methods are never blocking non static synchronized methods since once the lock is done per class (static) and once by instance (non-static)

Tagged with:
Mar 26

while non-static synchronized methods can only be locked per instance, static synchronized methods are locked by class, not by instance since such methods are always using the same class instance. Therefore one locked static synchronized method is locking all other static synchronized methods.

Tagged with:
Mar 25

The lock of synchronized methods is only done per instance not per class:

class Shop implements Runnable {

private int amount = 1;

private void sales_flow(int amount){

synchronized (this) {

check_stock(amount);
try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}
take_out_of_stock(amount);

}

}

public void check_stock(int amount){

if (this.amount>=amount)

System.out.println(“in Stock available for ” + Thread.currentThread().getName());

else

System.out.println(Thread.currentThread().getName() + ” can not sell this since not in Stock”);

}

public void take_out_of_stock (int amount){

this.amount=this.amount-amount;

if (this.amount>=0)

System.out.println(amount + ” pieces taken out of stock for ” + Thread.currentThread().getName());

else

System.out.println(“nothing in Stock anymore for ” + Thread.currentThread().getName());

}

public void run () { sales_flow(1); }

public static void main (String args []){

Shop a = new Shop();
Thread sale1 = new Thread(a);
sale1.setName(“Mary – Shop a”);
sale1.start();

Thread sale2 = new Thread(a);
sale2.setName(“Steve – Shop a”);
sale2.start();

Shop b = new Shop();
Thread sale3 = new Thread(b);
sale3.setName(“Brian – Shop b”);
sale3.start();

}

}

Output is:

in Stock available for Mary – Shop a
in Stock available for Brian – Shop b
1 pieces taken out of stock for Mary – Shop a
Steve – Shop a can not sell this since not in Stock
1 pieces taken out of stock for Brian – Shop b
nothing in Stock anymore for Steve – Shop a

This means that Shop a (Mary) and Shop b (Brian) can access the synchronized method on the same time while another instance of Shop a (Steve) can only access the method when the previous instance (Mary) has finished.


Tagged with:
Mar 24

We know already that with synchronized methods/blocks only one Thread can access them at once. This means that as soon as a method/block is accessed it is locked for others which are waiting and blocked for the moment.

Maybe you do not want me to repeat over and over again, but also here there is no guarantee, which waiting Thread will be chosen to be next.

A few more key-facts about locks I will provide within the next days

Tagged with:
Mar 23

in terms of synchronization there is not difference between static and non-static methods; both can be synchronized in the same way.

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs