Java Question :
Coming out of synchronization block automatically does notifyAll(). Is that the expected behavior?
I have tested it and it seems like 1. when a execution comes out of sync block , it does auto notifyAll() 2. When method itself is synchronized , it does auto notify() when it returns.( not notifyAll() )
Code :
public class Test {
public static void main(String[] args) throws InterruptedException {
MyThread lock = new MyThread();
new WatingThread(lock,1).start();
new WatingThread(lock,2).start();
//above tow threads would start and then wait for a lock
lock.start();
}
}
class MyThread extends Thread {
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("MyThread is trying to acquire the lock ..");
synchronized (this) {
System.out.println("MyThread has acquired the lock !!");
System.out.println("MyThread is Coming out of synch block..");
}
System.out.println("MyThread has released the lock !!");
}
}
class WatingThread extends Thread
{
private Object lock;
private int id;
public WatingThread(Object lock, int id )
{
this.lock = lock;
this.id = id;
}
@Override
public void run() {
System.out.println(String.format("[%d] : Check if lock is available ...",new Object[]{id}));
synchronized (lock) {
System.out.println(String.format("[%d] : Acquired the lock !!",new Object[]{id}));
try {
System.out.println(String.format("[%d] : Going to wait on lock.. ",new Object[]{id}));
lock.wait();
System.out.println(String.format("[%d] : Got notified !!!",new Object[]{id}));
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(String.format("[%d] :I am done !!",new Object[]{id}));
}
}
}
Output:
[2] : Check if lock is available ...
[2] : Acquired the lock !!
[1] : Check if lock is available ...
[2] : Going to wait on lock..
[1] : Acquired the lock !!
[1] : Going to wait on lock..
MyThread is trying to acquire the lock ..
MyThread has acquired the lock !!
MyThread is Coming out of synch block..
MyThread has released the lock !!
[1] : Got notified !!!
[1] :I am done !!
[2] : Got notified !!!
[2] :I am done !!
[2] : Check if lock is available ...
[2] : Acquired the lock !!
[1] : Check if lock is available ...
[2] : Going to wait on lock..
[1] : Acquired the lock !!
[1] : Going to wait on lock..
MyThread is trying to acquire the lock ..
MyThread has acquired the lock !!
MyThread is Coming out of synch block..
MyThread has released the lock !!
[1] : Got notified !!!
[1] :I am done !!
[2] : Got notified !!!
[2] :I am done !! – abhi Jun 06 '13 at 06:23