Inter Thread Communication in Java using wait notify methods

Inter Thread Communication in Java using wait notify methods

Inter-thread Communication is the process through which two or more Java threads communicate with each other. It is an important concept when you develop an application where two or more threads exchange some information. Classical example of inter-thread communication is the Producer consumer design pattern, where producer thread produces some information and notifies the consumer thread and then the consumer thread consumes the information and notifies the producer thread to produce next information. In this post we will see Inter Thread Communication in Java using wait notify methods.

Java threads can communicate with each other through wait(), notify(), notifyAll() methods. These methods are defined in java.lang.Object class. These methods have been implemented as final methods in Object class, to make them available in all the Java objects. All these methods can be called only from within a synchronized context.

  • wait() method Causes the current thread to wait until another thread invokes the notify()method or the notifyAll() method for this object.
  • notify() method wakes up a single thread that is waiting on this object’s monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object’s monitor by calling one of the wait methods.
  • notifyAll() method wakes up all threads that are waiting on this object’s monitor. A thread waits on an object’s monitor by calling one of the wait methods.
  • All these methods can throw IllegalMonitorStateException, if the current thread is not the owner of the object’s monitor

Here is an implementation of producer consumer design problem using wait() and notifyAll() methods in Java.

Following points should be noted in the above program.

  • As soon as a thread enters the synchronized block in produce() and consume() methods, it obtains a monitor(lock) on the fruits object. Once a thread enters the synchronized block, other threads cannot enter until the previous thread releases the lock and notifies the other threads.
  • In the above example, there are two producer threads and two consumer threads. Whichever producer thread(Producer-1 or Producer-2) enters the synchronized block first, locks the object and produces a fruit. Similarly whichever consumer thread(Consumer-1 or Consumer-2) enters the synchronized block first, locks the object and consumes the fruit. So the order of output can vary depending on the thread which gets CPU execution. But the output should maintain the flow that the consumer did not consume before the producer has produced it and producer did not produce before the consumer has consumed the available fruit.

Producer consumer implementation is an important Java interview question in Thread. So understand the concept clearly. Care should be taken when writing a multi-threaded program. Otherwise unexpected results may appear.

Hope you liked the post. If you have any comments, post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.
One comment

Add Comment

Required fields are marked *. Your email address will not be published.