Java thread state transition, WAITING TO BLOCK or RUNNABLE?

There seems to be a mismatch between SO consensus and almost every Java state flow diagram on the Internet; in particular, regarding state transition from WAITING after calling notify() or notifyAll() ...

  • WAITING never goes directly to RUNNABLE
  • Stream WAITING until it is notified ... Then it becomes BLOCKED ...
  • As soon as this thread is notified, it will not start ... This state ... is blocked.

So, the consensus on SO: the thread goes from WAITING to BLOCKED after calling notify() or notifyAll() ; The diagram below illustrates this transition in green.

Question

Why do most state diagrams on the Internet illustrate the transition from WAITING to RUNNABLE rather than BLOCKED ? A red image indicates an incorrect transition; Am I missing something?

enter image description here

+21
java multithreading java-threads thread-state
Feb 07 '15 at 4:36
source share
3 answers

Any diagram showing a notify call leading a stream from WAITING to RUNNABLE is incorrect (or uses an obscure label). Once the thread woke up from notify (or even from a false wakeup), it needs to reinstall the monitor of the object on which it was waiting. This state is BLOCKED .

The state of the thread for the thread blocked while waiting for the monitor to lock. A theme in a locked state expects a monitor lock to enter the synchronized block / method or re-enter the synchronized block / method after calling Object.wait .

This is explained in javadoc Object#notify() :

Awakened thread cannot act until the current thread releases the lock on this object.

and Object#wait()

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

+11
Feb 07 '15 at 4:55
source share
β€” -

Recently, I have been focusing on a problem.

as the Oracle Thread.State document says, we can use LockSupport.park () to put the current thread in the STANDBY or TIMED_WAITING state.

so when you try LockSupport.unpark (), the specified stream will return to "RUNNABLE" from "WAITING" / 'TIMED_WAITING', (I'm not sure if it will go through the "BLOCKED" state)

0
Jun 29 '15 at 16:20
source share

The stream is in WAITING state in the BLOCK state until it receives a monitor, notifying it and becomes RUNNABLE .

The same applies to TIMEDWAITING , it goes into the BLOCK state if the monitor is held by some other thread, although the specified time has passed (your diagram should be fixed)

0
Jul 02 '17 at 21:01
source share



All Articles