Java Thread: wait() and notify() simple example

wait() and notify() methods can be used to synchronize the access for a shared resource in multithreaded environment.  both of these methods should be called in a synchronized context.  that means either in synchronized block or synchronized method.

The following simple example will demonstrate how the wait() and notify() methods works. Keep this in your mind that this is a very simple example and the purpose is to demonstrate the behavior of wait() and notify() methods. if you are looking for a more advanced example, it is recommended to look at following article.

Advanced example with wait(), notify() and notifyAll()

 

 

The main thread of the Application class has created an instance of ThreadA and started it. After that the main thread invokes the wait() method on the ThreadA object.  In this situation, the main thread releases the object monitor of the ThreadA and let any other thread to continue work on it. main thread will wait(go to sleep mode) until some other thread calls the notify() (or notifyAll()) method on the same object. Since No other thread is waiting on ThreadA object and ThreadA is executing its run method.

ThreadA calls the notify() method on its own running instance. Once this is called any other thread that is waiting in this ThreadA object (currently running ThreadA object) will be notified. According to this example, main thread is waiting on ThreadA and it will be notified as follows.

Hello main thread. currently running instance of ThreadA has called the notify() method on its own( main thread is waiting for a notify() call on that object). Therefore the ThreadA may release the object monitor very soon and be ready to acquire it and resume with your work.

The invocation of notify() method will not immediately release the object monitor. It will be related once the synchronization bock has completed. In order to prove it, we have added 5 seconds of sleeping time/delay for the ThreadA after calling the notify() method.

Once the synchronization block is competed, ThreadA will release its object monitor and  it will be acquired by the main thread again. The main thread will resume its works.

If you run the above program, you will get the following output.

Main thread started 
ThreadA was started
Main thread is waiting for ThreadA 
ThreadA is running
ThreadA called notify() on its own
ThreadA sleeps for 5 seconds
ThreadA synchronization block completed
Main thread resumed again
Main thread completed

 

The important things to be noted!

after calling  the sleep method on a object, the currently running thread will release the already object lock of the related object. Then the calling thread will wait(or go to the sleep state) until the notify() method is called on the same object.

calling the notify() method will not release the object lock immediately. It will notifies some other waiting thread about this invocation and asks to be ready to acquire the object monitor as soon as it is released.  The object monitor will be released after completing the synchronization block of the currently running thread.

If there are multiple threads waiting on the objects and the notify() on the object has been invoked, the selection of thread to be notified will be determined by the thread scheduler.

 

notifyAll()

Ohh…. hold on please. i have forgot to mention about one of the important method here. that is notifyAll(). This will notify all the threads  that are waiting on a particular object. The order of notifying the threads will be determined by the thread scheduler.

One thought on “Java Thread: wait() and notify() simple example

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s