I'm trying to check how wait/notify works in java.
Code:
public class Tester {
public static void main(String[] args) {
MyRunnable r = new MyRunnable();
Thread t = new Thread(r);
t.start();
synchronized (t) {
try {
System.out.println("wating for t to complete");
t.wait();
System.out.println("wait over");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class MyRunnable implements Runnable {
public void run() {
System.out.println("entering run method");
synchronized (this) {
System.out.println("entering syncronised block");
notify();
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("leaving syncronized block");
}
System.out.println("leaving run method");
}
}
Output Returned
wating for t to complete
entering run method
entering syncronised block
//sleep called
leaving syncronized block
leaving run method
wait over
I was expecting when notify() is executed the wait will be over & System.out.println("wait over");
will get printed. But it seems it only gets printed when t
finished its run()
.
Object monitor locks need to be performed a single reference of the same lock...
In your example you are
waiting
on an instance of theThread
, but usingnotify
from theRunnable
. Instead, you should use a single, common lock object...for exampleOutput...
wait over
andleaving run method
could change positions depending on the thread scheduling.You could try putting the sleep out side the
synchronized
block. This will release the monitor lock allowing thewait
section to continue running (as it can't start until the lock is released)Note (as others pointed out as well) that you have to use the same object for locking/synchronizing in both threads.
If you want your main thread to continue immediately after
notify
is called, you have to relinquish the lock temporarily. Otherwisewait
will get called only after the secondary thread leaves thesynchronized
block. And it's never a good idea to keep a lock in a long running computation!One way how to achieve is to use
wait(int)
on the lock instead ofsleep
, becausewait
releases the synchronization lock temporarily:However, using these low-level primitives can be very error prone and I'd discourage from using them. Instead, I'd suggest you to use Java's high-level primitives for that. For example, you can use
CountDownLatch
which lets one thread wait until other threads count down to zero:Here you don't have to synchronize anything, the latch does everything for you. There are many other primitives you can use - semaphores, an exchanger, thread-safe queues, etc. Explorer the
java.util.concurrent
package.Perhaps even better solution is to use even higher level API, such as Akka provides. There you work with Actors or Software transactional memory, which can be composed easily and spare you of most of concurrency issues.
Answer to updated code :
From Thread.sleep() javadoc:
If you call Thread.sleep while inside a synchronized block, other threads won't be able to enter the synchronized block. You should never do time consuming tasks while in a synchronized block to avoid this.