0

I came across Java: notify() vs. notifyAll() all over again but still could not satisy myself.

xagyg explained it very well but in the end it became very complex to memorize the concept.

I am trying my best here with simple daily life example so that i and others can come back to this if any one forget. My source of understanding is answer by xagyg in above link but trying to simplyfy the things here.

Say two guys go to movie theatre and found its houseful. But then box office guy say Jon told them there is a ticket that has been reserved for president. If he does not come, he will sell it off. Then guys told to jon, ok we are waiting in hotel near by, please notify us when you get any info. These guys go to hotel and sleep. Now president does not turn up, now Jon has two options First is notify one of the guy and let other sleep. If he does that one can go movie while other will probably continue to sleep(till he doesn't get notified. I am assuming this guy didn't have sleep for a year :)). Another option is he notifies(awakens) both of them, choose any one of them(In actual java example program does not select but its vm/thread scheduler) for movie.In that case he will keep other guy in hotel room as he can create some kind of issues :(. Now once the show ends, this guy can go for next show if ticket is available. Consider ticket as lock, theatre as object. This what exactly notify and notifyAll does. So it is clear that notifAll is better over notify when in confusion

Now consider producer/consumer example.

say two consumer thread are waiting for production in store. Now what producer does, he produces two items in single go and exit. Now if producer use notify, only one thread can consumer while other will continue to wait for forever.

But if producer uses notifyAll() here, both thread can go for consumption one at a time

Let me know if my understanding is correct?

Community
  • 1
  • 1
M Sach
  • 33,416
  • 76
  • 221
  • 314
  • What's your question? – JB Nizet Mar 16 '14 at 11:22
  • @JB Nizet here i am looking if my understanding is correct or not?If not please correct me. – M Sach Mar 16 '14 at 11:23
  • 1
    notifyAll() wakes up all the waiting threads, whereas notify() only notifies one. Yes. That's what the javadoc says. In your example, Jon would not choose which guy should stay asleep. Instead, both guy would try to get the unique remaining ticket, and only one of them would get it. The other one would see that no ticket is remaining, and would keep waiting. – JB Nizet Mar 16 '14 at 11:26
  • possible duplicate of [Java: notify() vs. notifyAll() all over again](http://stackoverflow.com/questions/37026/java-notify-vs-notifyall-all-over-again) – Joe Mar 16 '14 at 11:29
  • @ JB Nizet I agree jon wont choose here it will by vm or thread scheduler when it translates to actual java example. you said "The other one would see that no ticket is remaining, and would keep waiting". when i convert this example to java, i think you mean waiting here waiting for lock not waiting to be notified as it has been already done – M Sach Mar 16 '14 at 11:37

1 Answers1

0

I don't think what you've written in the last statement is correct. Each time the producer produces an object it is supposed to notify a consumer, so if 2 objects are created it should invoke notify twice and not just once. This way if you use notify instead of notifyAll, you will still be able to get both consumer threads to consume

tinker
  • 1,396
  • 11
  • 20
  • I agree.i just tried to produce the scenario where notifyAll has upper hand over notify – M Sach Mar 17 '14 at 13:28
  • Accept the answer :) Yeah I don't think it's possible if you're following the traditional producer consumer. If the problem allows you to batch produce and then notify then you can have an advantage with notifyAll but that's situation dependent and not part of your original problem statement. – tinker Mar 17 '14 at 17:25