This post originated from an RSS feed registered with Java Buzz
by Javin Paul.
Original Post: Difference between wait vs notify vs notifyAll in Java
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
wait, notify, and notifyAll methods are used for inter thread
communication in Java. wait() allows a thread to check for a
condition, and wait if condition doesn't met, while notify() and notifyAll() method
informs waiting thread for rechecking condition, after changing state of shared
variable. One good example of how wait and notify method works is Producer
consumer problem, where one thread produces, and wait if bucket is full;
and other thread consumes and wait if bucket is empty. Both Producer and Consumer
thread, notify each other as well. Producer thread notifies consumer thread
after inserting an item in shared queue, while consumer thread notify producer,
after consuming item from queue. Though Both notify() and notifyAll()are used to notify waiting threads, waiting on
shared queue object, but there are some subtle
difference between notify and notifyAll in Java. Well, when we use notify(), only one
of the sleeping thread will get notification, while in case of notifyAll(), all
sleeping thread on that object will get notified. This concept confuses many
Java programmers, both beginners and experienced alike, Infact this is one of
three question which is very popular on wait and notify concept, along with why
wait and notify is defined in Object class and why
wait and notify called from synchronized method. In this article we will
focus on difference between wait, notify, and notifyAll method in Java.