This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() vs notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify() vs notifyAll()" Watch "notify() vs notifyAll()" New topic
Author

notify() vs notifyAll()

Royston Monteiro
Ranch Hand

Joined: Apr 25, 2008
Posts: 35

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?


Cheers,
Roy.
Sun Certified Java Programmer 1.5,
Sun Certified Web Component Developer 5.
"No pain. No Gain"
http://cs.nyu.edu/~rpm319
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
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

Joined: Apr 25, 2008
Posts: 35

doesnt the same thing happen with notifyAll() as well? both seem to give the same results
Taariq San
Ranch Hand

Joined: Nov 20, 2007
Posts: 192
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.
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
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
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18875
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
Hi.

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

Joined: Sep 28, 2004
Posts: 18875
    
  40

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

Joined: May 18, 2008
Posts: 29
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

Joined: Apr 25, 2008
Posts: 35

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

Joined: Nov 20, 2007
Posts: 192
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()
 
GeeCON Prague 2014
 
subject: notify() vs notifyAll()