YouTrack 2.0
The moose likes Threads and Synchronization and the fly likes Diffrence Between Notify & NotifyAll The Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Diffrence Between Notify & NotifyAll " Watch "Diffrence Between Notify & NotifyAll " New topic
Author

Diffrence Between Notify & NotifyAll

g.sathishkumar
Greenhorn

Joined: Jan 23, 2007
Posts: 15
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
methods.

Thanks,
Sathish Kumar.G
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1555
Satish: '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

It is partly correct, notifyAll() will notify all the threads and after that all the threads will contest for the monitor lock. The thread that gets the monitor lock will start the work and the rest will be waiting for the monitor lock.
The above difference is pretty big as the threads waiting for monitor will not require a notify() call to start processing but will start processing as soon as they get the monitor lock.
If you take the thread dump and see the state of the threads (although getting this dump at the correct time is somewhat tough),
  • In case of notify() only one thread will get to the RUNNING state and other will be in WAIT state
  • In case of notifyAll() only one thread will get to the RUNNING state and other will be in BLOCKED state


  • apigee, a better way to API!
    g.sathishkumar
    Greenhorn

    Joined: Jan 23, 2007
    Posts: 15
    Hi Nitesh ,
    Thanks dude... now i am clear about the concept......... your answer is pretty cool


    with thanks,
    G.sathish kumar.
     
    aspose file troubles
     
    subject: Diffrence Between Notify & NotifyAll
     
    Java Business RIA redefined!

    .