File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Difference between Notify and NotifyAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic

Difference between Notify and NotifyAll

Sathish Kumar Govindan

Joined: Jan 23, 2007
Posts: 18

Hello group,

I know that its a very lame question to ask experts like you but still
i cannot help myself asking the question that "What is the Diffrence
Between Notify & NotifyAll
" method in the Object Class.

Well so far my search on the Internet reveals
that 'notify()' method wakes up a single thread waiting on the object
and passes the control of the monitor to it. So far so good and for 'notifyAll()' it says that its will wake up all the threads waiting on the object and will select a thread to pass control to it. Well as per
me during that period the unselected thread will again go back to sleep
in the JVM scheduler list and they will need yet another call to
Notifty (or NotifyAll) in order to wake them up. So, as far as i see
there is no diffrence between notify & notifyall as they both will
result in waking up a single thread waiting on the Object.

If above assertions are indeed true than why to have two diffrent

Sathish Kumar.G

Certified on SCJP 5.0, OCPJWCD, Spring3.2, OCEJWSD
kamal shah

Joined: Feb 26, 2008
Posts: 18
notify will wake up one thread and notifyAll will wake up all threads which are waiting on that object.

if you have 3 threads which are waiting on same object.if you call notify one among the 3 will wake up and move to ready queue...but 2 still be in wait mode.

But if you call notifyAll all 3 will wake up and move to ready queue.

Regards,<br />Kamal<br /> <br />Life is nothing but a competition to be the criminal rather than the victim -Bertrand Russell
Arun Kumar Gaddam
Ranch Hand

Joined: May 05, 2007
Posts: 58
notify is called on an object to notify a thread which has locked that object,
where as a notifyAll will be called to notify all the threads which are waiting for that object.......
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42959
Please do not post the same question to multiple forums: CarefullyChooseOneForum

There are now answers in two forums, which is a waste of time and effort for the people who are trying to help you.

Let's continue the discussion in this duplicate thread.
[ February 27, 2008: Message edited by: Ulf Dittmer ]
I agree. Here's the link:
subject: Difference between Notify and NotifyAll
It's not a secret anymore!