其他分享
首页 > 其他分享> > 【9001期】线程状态及各状态下与锁和CPU的关系

【9001期】线程状态及各状态下与锁和CPU的关系

作者:互联网

Thread.State枚举类型中定义了线程的六种状态:NEW,RUNNABLE,BLOCKED,WAITING,TIMED_WAITING和TERMINATED。
线程在某一时刻只能拥有一种状态,但是在线程的整个生命周期,线程的状态会发生变化。

public enum State {
        NEW,
        RUNNABLE,
        BLOCKED,
        WAITING,
        TIMED_WAITING,
        TERMINATED;
}

各个状态的说明:

下图是关于线程间各状态切换的过程及发生状态切换的一些条件。

image

操作 操作前线程状态 操作后线程状态 是否出让CPU时间 是否需要先持有对象锁 是否释放对象锁
new Thread() NEW
Thread.start() NEW RUNNABLE
synchronized能得到对象锁 RUNNABLE RUNNABLE
synchronized无法得到对象锁 RUNNABLE BLOCKED
Thread.join() RUNNABLE WAITING
Thread.join(t) RUNNABLE TIMED_WAITING
Thread.sleep(t) RUNNABLE TIMED_WAITING
Object.wait() RUNNABLE WAITING
Object.wait(t) RUNNABLE TIMED_WAITING
Object.notify() / Object.notifyAll() RUNNABLE RUNNABLE
Lock.lock() RUNNABLE WAITING
Lock.tryLock(t) RUNNABLE TIMED_WAITING
LockSupport.park() RUNNABLE WAITING
LockSupport.parkNanos(t)/LockSupport.parkUntil(t) RUNNABLE TIMED_WAITING
执行结束/执行异常 RUNNABLE TERMINATED

 

吃水不忘挖井人:

 

标签:状态,RUNNABLE,WAITING,线程,9001,TIMED,CPU
来源: https://www.cnblogs.com/xfeiyun/p/15704671.html