When threads are waiting to access the critical section using synchronized block why the threads states show as BLOCKED and when the threads are waiting to access the critical section using Lock or semaphores threads state show as WAITING?
CodePudding user response:
This happens according to the java documentation.
From the javadoc for BLOCKED
:
... A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method ...
ReentrantLock
and Semaphore
use LockSupport.park()
internally, and, according to this javadoc, that corresponds to a WAITING
thread state:
A thread is in the waiting state due to calling one of the following methods:
- Object.wait with no timeout
- Thread.join with no timeout
- LockSupport.park