Threads and lifecycle question 8 of 180 mid
What is the difference between BLOCKED, WAITING and TIMED_WAITING in a thread dump?
- They are the same state under different names
- Correct: BLOCKED is waiting for a monitor; WAITING and TIMED_WAITING are waiting to be signalled, without and with a deadline
- BLOCKED means the thread was interrupted
- WAITING means the thread is on a CPU run queue
BLOCKED means the thread is trying to enter a synchronized block whose monitor someone else owns.
The dump names the owner, which makes lock contention and deadlock visible at a glance.
WAITING means the thread called Object.wait(), Thread.join(), LockSupport.park() or an untimed
Condition.await(), and will stay there until something signals it. TIMED_WAITING is the same with a
deadline.
The trap: a thread waiting on ReentrantLock shows as WAITING at LockSupport.park, never as
BLOCKED, because the lock is implemented with AbstractQueuedSynchronizer rather than a monitor. If
you grep a dump for BLOCKED to find contention, java.util.concurrent locks are invisible to you.