java线程状态及转换
所属分类 java
浏览量 933
初始(NEW) 新建线程 还未调用 start 方法
运行(RUNNABLE) 就绪(ready)和 运行中(running)调用start方法,进入就绪状态 ,获得CPU时间片后运行(running)
阻塞(BLOCKED) 线程阻塞于锁
等待(WAITING) 等待通知或中断
超时等待(TIMED_WAITING)
终止(TERMINATED) 线程已经执行完毕,再次调用start方法,抛 java.lang.IllegalThreadStateException 异常
等待队列 同步队列
同步队列里放争抢锁的线程
Thread.sleep(long millis) 当前线程进入TIMED_WAITING状态,不释放锁
Thread.yield() 放弃获取的CPU时间片,不释放锁 ,由运行状态变为就绪状态
thread.join()/thread.join(long millis) join基于wait实现
obj.wait() 当前线程释放锁,进入等待队列
obj.notify() 唤醒该对象监视器上等待的单个线程
obj.notifyAll 唤醒等待的所有线程
LockSupport.park()
LockSupport.parkNanos(long nanos)
LockSupport.parkUntil(long deadlines)
当前线程进入WAITING/TIMED_WAITING状态
对比wait方法,不需要获取锁就可以让线程进入WAITING/TIMED_WAITING状态,
通过LockSupport.unpark(Thread thread)唤醒
各种线程状态实例代码
https://gitee.com/dyyx/hellocode/blob/master/src/ThreadStateTest.java
ps -ef|grep ThreadStateTest
jstack PID
各种线程状态统计
jstack PID|grep java.lang.Thread.State: |sort|uniq -c|sort -rn
7 java.lang.Thread.State: TIMED_WAITING (sleeping)
7 java.lang.Thread.State: RUNNABLE
3 java.lang.Thread.State: WAITING (parking)
3 java.lang.Thread.State: WAITING (on object monitor)
2 java.lang.Thread.State: TIMED_WAITING (parking)
1 java.lang.Thread.State: TIMED_WAITING (on object monitor)
1 java.lang.Thread.State: BLOCKED (on object monitor)
完整线程堆栈信息
2020-12-11 08:18:58
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.121-b13 mixed mode):
"Attach Listener" #26 daemon prio=9 os_prio=31 tid=0x00007f88f485c000 nid=0x4b07 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"consumer3-timeout" #25 prio=5 os_prio=31 tid=0x00007f88f60be000 nid=0x9d03 waiting on condition [0x00007000020e7000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007955f6128> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2078)
at java.util.concurrent.LinkedBlockingQueue.poll(LinkedBlockingQueue.java:467)
at ThreadStateTest.consumerTimeout(ThreadStateTest.java:229)
at ThreadStateTest$$Lambda$17/1096979270.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"consumer2" #24 prio=5 os_prio=31 tid=0x00007f88f486e000 nid=0x9e03 waiting on condition [0x0000700001fe4000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007955f6128> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at ThreadStateTest.consumer(ThreadStateTest.java:214)
at ThreadStateTest$$Lambda$16/990368553.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"consumer1" #23 prio=5 os_prio=31 tid=0x00007f88f60bd800 nid=0xa003 waiting on condition [0x0000700001ee1000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007955f6128> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
at ThreadStateTest.consumer(ThreadStateTest.java:214)
at ThreadStateTest$$Lambda$15/1324119927.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"produce" #22 prio=5 os_prio=31 tid=0x00007f88f59df000 nid=0xa203 waiting on condition [0x0000700001dde000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadStateTest.doSleep(ThreadStateTest.java:104)
at ThreadStateTest.produce(ThreadStateTest.java:203)
at ThreadStateTest$$Lambda$13/2003749087.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"longSleepThread2" #21 prio=5 os_prio=31 tid=0x00007f88f59de000 nid=0xa403 waiting on condition [0x0000700001cdb000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadStateTest.doSleep(ThreadStateTest.java:104)
at ThreadStateTest.longSleep(ThreadStateTest.java:116)
at ThreadStateTest$$Lambda$14/1086906493.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"longSleepThread1" #20 prio=5 os_prio=31 tid=0x00007f88f59dd800 nid=0x5a03 waiting on condition [0x0000700001bd8000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadStateTest.doSleep(ThreadStateTest.java:104)
at ThreadStateTest.longSleep(ThreadStateTest.java:116)
at ThreadStateTest$$Lambda$12/424110450.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"joinRunTimeout" #19 prio=5 os_prio=31 tid=0x00007f88f50f9800 nid=0x5903 in Object.wait() [0x0000700001ad5000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795ab37e0> (a java.lang.Thread)
at java.lang.Thread.join(Thread.java:1257)
- locked <0x0000000795ab37e0> (a java.lang.Thread)
at ThreadStateTest.joinRunTimeout(ThreadStateTest.java:136)
at ThreadStateTest$$Lambda$11/295530567.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"joinRun" #18 prio=5 os_prio=31 tid=0x00007f88f50f8800 nid=0x5803 in Object.wait() [0x00007000019d2000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795a0cfe8> (a java.lang.Thread)
at java.lang.Thread.join(Thread.java:1249)
- locked <0x0000000795a0cfe8> (a java.lang.Thread)
at java.lang.Thread.join(Thread.java:1323)
at ThreadStateTest.joinRun(ThreadStateTest.java:124)
at ThreadStateTest$$Lambda$10/1283928880.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"reentranLockTry2" #17 prio=5 os_prio=31 tid=0x00007f88f59d7000 nid=0xa703 waiting on condition [0x00007000018cf000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007955f5bd8> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireNanos(AbstractQueuedSynchronizer.java:934)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireNanos(AbstractQueuedSynchronizer.java:1247)
at java.util.concurrent.locks.ReentrantLock.tryLock(ReentrantLock.java:442)
at ThreadStateTest.reentranLockTry(ThreadStateTest.java:175)
at ThreadStateTest$$Lambda$9/1915318863.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"reentranLockTry1" #16 prio=5 os_prio=31 tid=0x00007f88f60b8800 nid=0xa803 waiting on condition [0x00007000017cc000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadStateTest.doSleep(ThreadStateTest.java:104)
at ThreadStateTest.reentranLockTry(ThreadStateTest.java:185)
at ThreadStateTest$$Lambda$8/250421012.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"reentranLock2" #15 prio=5 os_prio=31 tid=0x00007f88f60b8000 nid=0x5503 waiting on condition [0x00007000016c9000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007955f5ba8> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
at ThreadStateTest.reentranLock(ThreadStateTest.java:162)
at ThreadStateTest$$Lambda$7/455659002.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"reentranLock1" #14 prio=5 os_prio=31 tid=0x00007f88f59d6000 nid=0x3e03 waiting on condition [0x00007000015c6000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadStateTest.doSleep(ThreadStateTest.java:104)
at ThreadStateTest.reentranLock(ThreadStateTest.java:164)
at ThreadStateTest$$Lambda$6/1705736037.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"sync2" #13 prio=5 os_prio=31 tid=0x00007f88f59d5800 nid=0x4003 waiting for monitor entry [0x00007000014c3000]
java.lang.Thread.State: BLOCKED (on object monitor)
at ThreadStateTest.sync(ThreadStateTest.java:155)
- waiting to lock <0x00000007955f4870> (a java.lang.Object)
at ThreadStateTest$$Lambda$5/245257410.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"sync1" #12 prio=5 os_prio=31 tid=0x00007f88f4832000 nid=0x4103 waiting on condition [0x00007000013c0000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadStateTest.doSleep(ThreadStateTest.java:104)
at ThreadStateTest.sync(ThreadStateTest.java:155)
- locked <0x00000007955f4870> (a java.lang.Object)
at ThreadStateTest$$Lambda$4/1406718218.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"yieldLoop" #11 prio=5 os_prio=31 tid=0x00007f88f60b7000 nid=0x3a03 runnable [0x00007000012bd000]
java.lang.Thread.State: RUNNABLE
at java.lang.Thread.yield(Native Method)
at ThreadStateTest.yieldLoop(ThreadStateTest.java:148)
at ThreadStateTest$$Lambda$3/1826771953.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" #8 daemon prio=9 os_prio=31 tid=0x00007f88f4858800 nid=0x4303 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #7 daemon prio=9 os_prio=31 tid=0x00007f88f483e000 nid=0x4403 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #6 daemon prio=9 os_prio=31 tid=0x00007f88f483d000 nid=0x3403 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #5 daemon prio=9 os_prio=31 tid=0x00007f88f5024000 nid=0x3303 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=31 tid=0x00007f88f5023000 nid=0x4703 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=31 tid=0x00007f88f4824800 nid=0x4e03 in Object.wait() [0x0000700000aa5000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795588ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x0000000795588ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" #2 daemon prio=10 os_prio=31 tid=0x00007f88f4821800 nid=0x2c03 in Object.wait() [0x00007000009a2000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x0000000795586b68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x0000000795586b68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"main" #1 prio=5 os_prio=31 tid=0x00007f88f6004000 nid=0x1203 waiting on condition [0x000070000028a000]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at ThreadStateTest.main(ThreadStateTest.java:92)
"VM Thread" os_prio=31 tid=0x00007f88f4821000 nid=0x5103 runnable
"GC task thread#0 (ParallelGC)" os_prio=31 tid=0x00007f88f6007800 nid=0x1b07 runnable
"GC task thread#1 (ParallelGC)" os_prio=31 tid=0x00007f88f5800000 nid=0x1c03 runnable
"GC task thread#2 (ParallelGC)" os_prio=31 tid=0x00007f88f5802000 nid=0x2b03 runnable
"GC task thread#3 (ParallelGC)" os_prio=31 tid=0x00007f88f6008800 nid=0x5303 runnable
"VM Periodic Task Thread" os_prio=31 tid=0x00007f88f602e800 nid=0x3703 waiting on condition
JNI global references: 334
java thread join实现原理
java线程状态
Lock和synchronized的区别
Java线程状态中BLOCKED和WAITING的区别
上一篇
下一篇
云原生时代的Java
进程io监控命令iopp
Stack ArrayDeque LinkedList
LockSupport简介
ReentrantLock原理
基于 wait 和 notifyAll 实现简单的阻塞队列