• Post Reply Bookmark Topic Watch Topic
  • New Topic

notify and notify all

 
harish shankarnarayan
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
suppose when a synchonized method executes wait().that thtraed goes to the wait state, and leaves the monitor.
suppose in the same way 3 threads are ter in the waiting state.
now when another thread which has the monitor
1:gives notify() which thread in the wait set acqiires the monitor,
2:gives notifyAll()which thread in the wait set acqiires the monitor,

wats the exact difference can anyone explain wat happens.
plz do explain one by one with example.

thanks.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Waking up and acquiring the monitor are two differen tthings. When a thread calls notify, one of the threads (the OS choice) will wake up. When a thread calls notifyAll, all of the threads will wake up.

After a thread is awake it must first acquire the monitor before it can perform any act since it is still within the synchronization block. In the case of notify, there is only 1 thread awake, so as soon as the notifying thread releases the lock, that thread should get the lock. In the case of notify all, there are multiple threads awake. As soon as the nofityAll thread releases the lock, each awake thread will contend for it.
 
soudamini.s shirkhedkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not very sure what exactly happens but my understanding is:

If there are several threads wishing to use the same object and are currently executing wait() because some other thread has control of the monitor and hence it is executing the synchronised code block/method associated with the object:

A call to notify() method wakes up the thread that called wait() method first.If priorities are given to the threads, it can also wake up the thread with highest priority.

A call to notifyAll() wakes up all the threads that are waiting for access. Scheduler will then schedule the next thread to be run depending upon the priorities or the threads might contend upon themselves for access to the monitor.
 
Mukesh Mittal
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think, if the priority is same then schedular will decide.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The choise is up to the OS and may be loosely related to priorities, but not directly. You will have to consult OS documentation if you wish to know the likelihood of any particular thread getting the monitor.

The point is, notifyAll will end up with 1 thread waking up and getting the lock, the rest awake and blocking for the lock. notify will end up with 1 thread getting the lock, the rest still asleep and not carying about the lock.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!