java线程状态学习
线程状态
NEW
新建,未start的线程- 继承Thread
- 实现Runnable
- 实现Callable注入FutureTask
RUNNABLE
可运行,cpu调度运行(运行中,就绪)- start()
WAITING
无限期等待,主动进入- Object.wait()
- Thread.join() 其会用Object.wait()实现
- LockSupport.park()
- 其他使用上面三个来同步控制的(ReentrantLock)
TIME_WAITING
有限等待,主动进入- Object.wait(long_time)
- Thread.sleep(long_time)
- Thread.join(long_time) 其会用Object.wait()实现
- LockSupport.parkNanos(long_time)
- LocksUPPORT.parkUntil(long_deadtime) deadtime - 终止时间点
- 其他使用上面四个来同步控制的
BLOCKED
阻塞,被动进入- enter synchronized
- reenter synchronzied (获取监视器对象锁后Object.wait()后被notify唤醒重新进入synchronized,再次去抢占获取监视器对象锁)
TERMINATED
终止- 程序跑完
- 抛出异常
堆栈线程状态
java线程状态 | 堆栈线程状态 |
---|---|
NEW | 未运行无此状态 |
RUNNABLE | RUNNABLE |
WAITING (wait(),join()) | WAITING (on object monitor) |
WAITING (park()) | WAITING (parking) |
TIMED_WAITING (sleep(t)) | TIMED_WAITING (sleeping) |
TIMED_WAITING (wait(t),join(t)) | TIMED_WAITING (on object monitor) |
TIMED_WAITING (parkNanos(t),parkUntil(t) | TIMED_WAITING (parking) |
BLOCKED (synchronzied) | BLOCKED (on object monitor) |
转载请注明 from tomsun28