synchronized
synchronized 竞争object
synchronized(object)由于object竞争不到,导致只有一个线程运行临界区内,其他线程等待,所有线程的状态如下:
"mythread-1" #13 prio=5 os_prio=31 cpu=5967.27ms elapsed=6.03s tid=0x00007fe03c054000 nid=0x9c03 runnable [0x0000700005bc0000]
java.lang.Thread.State: RUNNABLE
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:64)
- locked <0x000000070fea5490> (a java.lang.Object)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
"mythread-2" #14 prio=5 os_prio=31 cpu=0.34ms elapsed=6.03s tid=0x00007fe03b092800 nid=0x9903 waiting for monitor entry [0x0000700005cc3000]
java.lang.Thread.State: BLOCKED (on object monitor)
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:64)
- waiting to lock <0x000000070fea5490> (a java.lang.Object)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
所以,由于synchronized,竞争锁时,引起的线程阻塞,线程状态为BLOCKED
,且等待获取锁x;此时运行的线程栈信息中,表示线程已经获取了锁x(即locked <0x000000070fea5490>
)。
- 资源满足的线程:
- 线程状态:
RUNNABLE
- 临界状态:
locked <0x000000070fea5490>
- 线程状态:
- 阻塞的线程:
- 线程状态:
BLOCKED (on object monitor)
- 临界状态:
waiting to lock <0x000000070fea5490>
- 线程状态:
synchronized 竞争到锁后,条件不满足调用Object.wait()
synchronized(object)运行到临界区内,但是由于其他逻辑条件不满足,使用object.wait()等待资源满足条件,此状态的线程栈信息如下:
"mythread-1" #13 prio=5 os_prio=31 cpu=6452.54ms elapsed=6.51s tid=0x00007f94c982b000 nid=0xa103 runnable [0x000070000942d000]
java.lang.Thread.State: RUNNABLE
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:75)
- locked <0x000000070fea5570> (a java.lang.Object)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
"mythread-2" #14 prio=5 os_prio=31 cpu=0.13ms elapsed=6.51s tid=0x00007f94cc884800 nid=0x6303 in Object.wait() [0x0000700009530000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(java.base@12.0.1/Native Method)
- waiting on <0x000000070fea5570> (a java.lang.Object)
at java.lang.Object.wait(java.base@12.0.1/Object.java:326)
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:67)
- locked <0x000000070fea5570> (a java.lang.Object)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
所以,synchronized 竞争到锁后,条件不满足调用Object.wait()
,此时的线程状态为:
- 资源满足的线程:
- 线程状态:
RUNNING
- 临界状态:locked
- 线程状态:
- 阻塞的线程:
- 线程状态:
WAITING (on object monitor)
- 临界状态:
waiting on
- 线程状态:
synchronized 竞争到锁后,条件不满足调用Object.wait(timeout)
synchronized(object)运行到临界区内,但是由于其他逻辑条件不满足,使用object.wait(timeout)等待资源满足条件,此状态的线程栈信息如下:
"mythread-1" #13 prio=5 os_prio=31 cpu=4391.51ms elapsed=4.45s tid=0x00007fc0f8001000 nid=0x6503 runnable [0x000070000a0e2000]
java.lang.Thread.State: RUNNABLE
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:75)
- locked <0x000000070fea5580> (a java.lang.Object)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
"mythread-2" #14 prio=5 os_prio=31 cpu=0.10ms elapsed=4.45s tid=0x00007fc0f7000000 nid=0xa203 in Object.wait() [0x000070000a1e5000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(java.base@12.0.1/Native Method)
- waiting on <0x000000070fea5580> (a java.lang.Object)
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:67)
- locked <0x000000070fea5580> (a java.lang.Object)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
所以,synchronized 竞争到锁后,条件不满足调用Object.wait(timeout)
,此时的线程状态为:
- 资源满足的线程:
- 线程状态:
RUNNING
- 临界状态:locked
- 线程状态:
- 阻塞的线程:
- 线程状态:
TIMED_WAITING (on object monitor)
- 临界状态:
waiting on
- 线程状态:
LockSupport.park
LockSupport.park()
线程运行阻塞原语LockSupport.park()
,线程栈信息如下:
"mythread-1" #13 prio=5 os_prio=31 cpu=0.24ms elapsed=3.81s tid=0x00007f994883a800 nid=0xa003 waiting on condition [0x00007000072a7000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@12.0.1/Native Method)
at java.util.concurrent.locks.LockSupport.park(java.base@12.0.1/LockSupport.java:324)
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:83)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
线程阻塞时:
1. 线程状态:`WAITING (parking)`
2. 临界状态: 无
LockSupport.park(Object)
线程运行阻塞原语LockSupport.park(Object)
,线程栈信息如下:
"mythread-1" #13 prio=5 os_prio=31 cpu=0.52ms elapsed=4.31s tid=0x00007ff8a2804000 nid=0x9c03 waiting on condition [0x0000700006e5a000]
java.lang.Thread.State: WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@12.0.1/Native Method)
- parking to wait for <0x000000070fea5400> (a java.lang.Object)
at java.util.concurrent.locks.LockSupport.park(java.base@12.0.1/LockSupport.java:194)
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:82)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
线程阻塞时:
1. 线程状态:`WAITING (parking)`
2. 临界状态:`parking to wait for <0x000000070fea5400>`
LockSupport.parkNanos(ns)
"mythread-1" #13 prio=5 os_prio=31 cpu=0.56ms elapsed=6.04s tid=0x00007f9fdc85d800 nid=0x9c03 waiting on condition [0x0000700004cd4000]
java.lang.Thread.State: TIMED_WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@12.0.1/Native Method)
at java.util.concurrent.locks.LockSupport.parkNanos(java.base@12.0.1/LockSupport.java:359)
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:84)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
线程阻塞时:
1. 线程状态:`TIMED_WAITING (parking)`
2. 临界状态:无
LockSupport.parkNanos(Object,ns)
"mythread-1" #13 prio=5 os_prio=31 cpu=0.63ms elapsed=8.48s tid=0x00007f81f2085800 nid=0x5f03 waiting on condition [0x000070000a179000]
java.lang.Thread.State: TIMED_WAITING (parking)
at jdk.internal.misc.Unsafe.park(java.base@12.0.1/Native Method)
- parking to wait for <0x000000070fea5790> (a java.util.concurrent.locks.ReentrantLock)
at java.util.concurrent.locks.LockSupport.parkNanos(java.base@12.0.1/LockSupport.java:235)
at win.hgfdodo.SynchronizedAndLockStackStatus.run(SynchronizedAndLockStackStatus.java:84)
at java.lang.Thread.run(java.base@12.0.1/Thread.java:835)
线程阻塞时:
1. 线程状态:`TIMED_WAITING (parking)`
2. 临界状态:`parking to wait for `
总结
阻塞线程状态总结如下:
阻塞方式 | 线程状态 | 临界状态 |
---|---|---|
synchronized(object) | BLOCKED (on object monitor) | wait to lock |
Object.wait() | WAITING (on object monitor) | waiting on |
Object.wait(timeout) | TIMED_WAITING (on object monitor) | waiting on |
LockSupport.park() | WAITING (parking) | 无 |
LockSupport.park(Object) | WAITING (parking) | parking to wait for |
LockSupport.parkNanos(ns) | TIMED_WAITING (parking) | 无 |
LockSupport.parkNanos(Object,ns) | TIMED_WAITING (parking) | parking to wait for |
ReentrantLock是基于
LockSupport
实现阻塞线程,所以阻塞时,线程状态和锁状态与LockSupport.park
系列情况一致,根据请求lock使用不同的方法时,对应不同的LockSupport
方法,对应着不同的线程状态和临界状态。
--Edited from Rpc