This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notify() vs notifyAll()

 
Royston Monteiro
Ranch Hand
Posts: 35
Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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?
 
Taariq San
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • 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
Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jolly Tiwari
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Jack Crifer
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi.

But in this case,notify() make all the waiting threads wake up too.
 
Henry Wong
author
Marshal
Pie
Posts: 20995
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Linux Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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()
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic