• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

notify() vs notifyAll()

 
Ranch Hand
Posts: 35
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi folks,


According to my understanding, notify() will notify only one thread, while notifyAll() will notify all threads waiting on that object. In this code, when notify() is called, all the threads seem to be notified as
"after wait()" is printed thrice. How can this be? shouldnt it get printed only once?
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to my understanding, notify() will notify only one thread,



That's right, but the lock is released and the 3 waiting threads each get a turn to lock and release.
Try adding "Thread.sleep(5000);" after wait to make this clearer.
[ June 05, 2008: Message edited by: Taariq San ]
 
Royston Monteiro
Ranch Hand
Posts: 35
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
doesnt the same thing happen with notifyAll() as well? both seem to give the same results
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Royston Monteiro:
doesnt the same thing happen with notifyAll() as well? both seem to give the same results




Yes indeed.
Only 1 thread will get a lock at once and notifyAll() won't change that, it just wakes up all the waiting threads who then have to compete for the lock.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Taariq San:


That's right, but the lock is released and the 3 waiting threads each get a turn to lock and release.
Try adding "Thread.sleep(5000);" after wait to make this clearer.

[ June 05, 2008: Message edited by: Taariq San ]



Buddy, when 3 threads are in the blocked or waiting state then only one of them can be notified as there is a single notify() call.
So, i think only one of them will wake up and acquire the lock and proceed further. How come two other threads are getting up.

Please correct me where i am wrong and please do explain it because output is according to you

Regards

Jolly
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

In this code, when notify() is called, all the threads seem to be notified as "after wait()" is printed thrice. How can this be? shouldnt it get printed only once?



Basically, this is a *bad* example... The Thread object is used internally to implement the join() method. The join() method will call wait(), until the thread is no longer alive (reported by the isAlive() method). And the thread cleanup code, will call the notifyAll() method on the thread object, once the thread is mark as no longer alive.

Henry
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.

But in this case,notify() make all the waiting threads wake up too.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jack Crifer:
Hi.

But in this case,notify() make all the waiting threads wake up too.



No. It is *not*. Take a look at the code.



The notify() method will wakeup one thread from the wait() method. Then the run() method completes. At this point, the thread completes all its internal cleanup code -- including calling notifyAll() to wake up the other threads in the join() method.

The other threads are being woken up by this notifyAll() method call. This is a *bad* artifact of waiting on the same object as the code that implements the join() method.

Henry
 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Henry

I think I got the point, the instance of Thread cleanup code , it alway call notifyAll() method of that object.

And can you explain the details of join() method which the Thread object is used internally to implement. Is run() method calls it?

Best Regards.
 
Royston Monteiro
Ranch Hand
Posts: 35
Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry. I think i got it too . From what I understood, the difference between the notify() and the notifyAll() is this:

When notifyAll() is called, all the threads waiting on a particular thread object move from the waiting to the runnable state.
In the case of the notify() method, only one "lucky" thread moves from waiting to runnable(which one exactly is not predictable). Then, after this "lucky" thread finishes its run() and all the thread cleanup code, notifyAll() is called. Consequently all the threads that were waiting move to runnable.

Is this right?
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


Basically, this is a *bad* example... The Thread object is used internally to implement the join() method. The join() method will call wait(), until the thread is no longer alive (reported by the isAlive() method). And the thread cleanup code, will call the notifyAll() method on the thread object, once the thread is mark as no longer alive.

Henry



Thanks for that Henry, I assumed that notify() was called, interesting to see it's notifyAll()
 
I'm still in control here. LOOK at this tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic