BLOCKED. 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.
You stop your car in front of his house. You wait for 10 minutes, but your neighbor still doesn't come out. You go ahead and start driving to work, as you don't want to be delayed on your first day. Now this is TIMED_WAITING.
A Blocked state will occur whenever a thread tries to acquire lock on object and some other thread is already holding the lock. Once other threads have left and its this thread chance, it moves to Runnable state after that it is eligible pick up work based on JVM threading mechanism and moves to run state.
Wait() method releases lock during Synchronization. Sleep() method does not release the lock on object during Synchronization. Wait() should be called only from Synchronized context.
The difference is relatively simple.
In the BLOCKED
state, a thread is about to enter a synchronized
block, but there is another thread currently running inside a synchronized
block on the same object. The first thread must then wait for the second thread to exit its block.
In the WAITING
state, a thread is waiting for a signal from another thread. This happens typically by calling Object.wait()
, or Thread.join()
. The thread will then remain in this state until another thread calls Object.notify()
, or dies.
A thread goes to wait state once it calls wait()
on an Object. This is called Waiting State. Once a thread reaches waiting state, it will need to wait till some other thread calls notify()
or notifyAll()
on the object.
Once this thread is notified, it will not be runnable. It might be that other threads are also notified (using notifyAll()
) or the first thread has not finished his work, so it is still blocked till it gets its chance. This is called Blocked State. A Blocked state will occur whenever a thread tries to acquire lock on object and some other thread is already holding the lock.
Once other threads have left and its this thread chance, it moves to Runnable state after that it is eligible pick up work based on JVM threading mechanism and moves to run state.
The important difference between the blocked and wait states is the impact on the scheduler. A thread in a blocked state is contending for a lock; that thread still counts as something the scheduler needs to service, possibly getting factored into the scheduler's decisions about how much time to give running threads (so that it can give the threads blocking on the lock a chance).
Once a thread is in the wait state the stress it puts on the system is minimized, and the scheduler doesn't have to worry about it. It goes dormant until it receives a notification. Except for the fact that it keeps an OS thread occupied it is entirely out of play.
This is why using notifyAll is less than ideal, it causes a bunch of threads that were previously happily dormant putting no load on the system to get woken up, where most of them will block until they can acquire the lock, find the condition they are waiting for is not true, and go back to waiting. It would be preferable to notify only those threads that have a chance of making progress.
(Using ReentrantLock instead of intrinsic locks allows you to have multiple conditions for one lock, so that you can make sure the notified thread is one that's waiting on a particular condition, avoiding the lost-notification bug in the case of a thread getting notified for something it can't act on.)
Simplified perspective for interpreting thread dumps:
Blocked- Your thread is in runnable state of thread life cycle and trying to obtain object lock. Wait- Your thread is in waiting state of thread life cycle and waiting for notify signal to come in runnable state of thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With