Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java thread state transition, WAITING to BLOCKED, or RUNNABLE?

There seems to be a discrepancy between SO consensus and nearly every Java thread state diagram on the Internet; specifically, regarding thread state transition from WAITING after notify() or notifyAll() is invoked...

  • WAITING never goes directly to RUNNABLE
  • The thread is WAITING until it is notified...Then it becomes BLOCKED...
  • Once this thread is notified, it will not be runnable...This is..Blocked State.

So the concensus on SO is: a thread transitions from WAITING to BLOCKED after invoking notify() or notifyAll(); diagram below illustrates this transition in green.

Question

Why do most state diagrams on the web illustrate the transition from WAITING to RUNNABLE, not BLOCKED? Depiction in red shows the incorrect transition; am I missing something?

enter image description here

like image 677
raffian Avatar asked Feb 07 '15 04:02

raffian


People also ask

Is it legal to have a thread state transition from waiting to running if it is legal How does the transition occur if not why?

Illegal. A thread can only transition to BLOCKED from RUNNING. It cannot execute any statements when still in a queue (i.e. the ready queue). h.

What is the difference between waiting and blocked state of a thread?

A thread is in the waiting state when it wants to wait on a signal from another thread before proceeding. Once this signal is received, it becomes runnable. A thread moves to the blocked state when it wants to access an object that is being used (locked) by another thread.

How can a thread go from waiting to runnable state?

when sleep() is called on thread it goes from running to waiting state and can return to runnable state when sleep time is up.

Which method changes the state of thread from waiting to ready?

The start() method of the Thread class is used to initiate the execution of a thread and it goes into runnable state and the sleep() and wait() methods of the Thread class sends the thread into non runnable state. After non runnable state, thread again comes into runnable state and starts its execution.


1 Answers

Any diagram that shows a notify invocation bringing a thread from WAITING to RUNNABLE is wrong (or is using an unclarified shortcut). Once a thread gets awoken from a notify (or even from a spurious wakeup) it needs to relock the monitor of the object on which it was waiting. This is the BLOCKED state.

Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

This is explained in the javadoc of Object#notify():

The awakened thread will not be able to proceed until the current thread relinquishes the lock on this object.

and Object#wait()

The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

like image 52
Sotirios Delimanolis Avatar answered Oct 07 '22 08:10

Sotirios Delimanolis