• Post Reply Bookmark Topic Watch Topic
  • New Topic

How the state transition takes place when notify or notifyall is called?

 
BadriNath Ojha
Greenhorn
Posts: 12
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wanted to know what happens when notify and notifyall is called on an object.
I read from some blog that when notify() is called only one thread out of many which called the wait() on the object is made awake from WAITING state and made RUNNABLE directly and when notifyall() is called then all the threads which called wait() are made awake but only one choosen by JVM scheduler and shifted to RUNNABLE state and others in BLOCKED state and not in WAITING state.
Please let me know.
 
Henry Wong
author
Sheriff
Posts: 22531
109
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BadriNath Ojha wrote:I wanted to know what happens when notify and notifyall is called on an object.
I read from some blog that when notify() is called only one thread out of many which called the wait() on the object is made awake from WAITING state and made RUNNABLE directly and when notifyall() is called then all the threads which called wait() are made awake but only one choosen by JVM scheduler and shifted to RUNNABLE state and others in BLOCKED state and not in WAITING state.
Please let me know.


Correct. Calling the notify() method will wake up one of the threads, that is waiting on the object, and put it in the block state (as it needs to reacquire the synchronization lock). Calling notifyAll() does that to all the threads waiting on the object.

Once a thread reacquires the synchronization lock, it becomes runnable, and can return from the wait() method.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!