Forums Register Login

Concept of wait(), notify() and notifyAll()

+Pie Number of slices to send: Send
Hi,

I am having a really hard time understanding the concept of wait(), notify() and notifyAll(). I have read the articles in the net as well as the books but I havn't got the concept behind the same. Would someone like to explain me the concept with some easy to understand examples. ?

Thanks and Regards,
Siddharth (Sid)
+Pie Number of slices to send: Send
best place would be javadocs, did you go through that too? let us know what exactly you didn't get..Doing some hands-on will make stuff more clear, did you try that too?
+Pie Number of slices to send: Send
Search previous posts on coderanch. You will find many on them.
+Pie Number of slices to send: Send
What really helped me a few years ago was an article I read that made the analogy between object monitors and airplane bathrooms: http://www.nyu.edu/classes/jcf/g22.3033-007_sp01/handouts/g22_3033_h53.htm

If you have specific questions, please let us know.
+Pie Number of slices to send: Send
 

Anurag Verma wrote:best place would be javadocs, did you go through that too? let us know what exactly you didn't get..Doing some hands-on will make stuff more clear, did you try that too?



Yes I did went through the Java docs. What exactly does this line "The wait() method releases the lock prior to waiting and reacquires the lock prior to returning from the wait() method. The wait() method is actually tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism. In other words, it is not possible for us to implement the wait() method purely in Java: it is a native method.." mean ?

Also I could not understand the producer / consumer problem.



What is the purpose of taskQueue.notifyAll(); specifically ? Similarly in the below code:




Could you please explain me the producer / consumer e.g. using wait / notify / notifyAll ? Step-by-step what is happening specifically the code inside the synchronized block and after the while loop.

Thanks and Regards,
Sid
+Pie Number of slices to send: Send
 

Siddharth Bhargava wrote:
Yes I did went through the Java docs. What exactly does this line "The wait() method releases the lock prior to waiting and reacquires the lock prior to returning from the wait() method. The wait() method is actually tightly integrated with the synchronization lock, using a feature not available directly from the synchronization mechanism. In other words, it is not possible for us to implement the wait() method purely in Java: it is a native method.." mean ?



In my opinion, the quote doesn't really say anything that useful. Who cares if the wait() method can't be done purely in Java? Isn't that just an implementation detail?

Anyway, if you ever use any other threading environment, such as Solaris threads, POSIX threads, or even Windows threads, you have use this feature / functionality before. It goes by the name of "condition variable". And it is tightly integrated with an mutually exclusive lock (mutex), which in Java is implemented via synchronization. Java also implements the mutex and condition variable differently but that's for a different discussion.

What a condition variable does is atomically send a notification (signal) from one thread to another. To be atomic, it must use the mutex (must own the lock). And for the thread that must wait for the notification, it must free the lock -- or else, it won't be possible for the send thread to own the lock to send the notification.

And unfortunately, this can't be done without tightly integrating those two features. Not integrating it, will make it possible for notifications to be lost... and unfortunately, this may be a bit hard to envision.


Anyway, perhaps you should do some research on "condition variables" in general. Since, this feature is available with practically all threading environments, you have other documentation to rely on here (don't need to be specific to Java).

Henry
(1 cow) 1
+Pie Number of slices to send: Send
wait() -> you eat at your reserved table, while all the other customers wait outside for an empty table

notify() -> when you are done, the waiter goes out, picks one customer randomly, tells them an empty table is available. The rest continue to wait.

notifyAll() -> when you are done, the waiter just shouts "EMPTY TABLE" and this happens:


The table is the thing being synchronized here.
If it wasn't, you'd end up sitting next to a family with 3 brats, eating their cereal while they enjoy your fine (expensive) wine!
(1 cow) 1
+Pie Number of slices to send: Send
 

Karthik Shiraly wrote:The table is the thing being synchronized here.


Wow. The first use case I've ever seen of

1
+Pie Number of slices to send: Send
Good one, Steffe!
Looks like this one went "unchequed".
(1 cow)
+Pie Number of slices to send: Send
The post with the YouTube video is so funny

Anyway, in reality, things should be a little more orderly:
  • call notify() if it doesn’t matter which customer (thread) gets to use the empty table. The thread scheduler will select one customer at random.
  • call notifyAll() if the empty table is a tall table and only tall customers (threads) should use it. Every customer will then fight for the table and eventually, a tall customer will get to use the table.



  • I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com


    reply
    reply
    This thread has been viewed 2899 times.
    Similar Threads
    Doubt about using notify()
    Threads wait/notify Concept
    consumes no CPU cycles?
    Mapping Collection in Hibernate.
    Trying out thread sample
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    More...

    All times above are in ranch (not your local) time.
    The current ranch time is
    Apr 16, 2024 08:39:11.