NEW 初始
构造一个线程实例后、启动前,线程处于该状态
RUNNABLE 运行
该状态包含了经典线程模型的两种状态:就绪(Ready)、运行(Running):
就绪状态表示有资格运行,但如果一直没拿到时间片,就一直是就绪状态
- 初始状态的线程,start()方法被调用后,就进入RUNNABLE状态,表示就绪(Ready);并开始等待CPU时间片;
- 等待或阻塞状态结束,如sleep()结束、或其他线程join()结束、拿到锁,也会进入就绪状态
- 线程时间片用完,线程的yield()方法会被调用,线程随即进入就绪状态
当线程调度选中该线程、并分配了CPU时间片后,该线程尽管处于Runnable状态,但实际上是运行(Running);
- 这是线程进入运行状态的唯一方式
BLOCKED 阻塞
通常与锁有关系,表示线程正在获取有锁控制的资源,比如进入synchronized代码块,获取ReentryLock等
WAITING
进入该状态的线程,等待被显式唤醒(其他线程发出通知或中断,如notify/notifyAll
, 或者另外的线程终止如join
的情况),否则处于无限期等待
TIMED_WAITING 超时等待
该状态不同于WAITING,它可以在指定的时间后自动唤醒;
- 处于这种状态的线程不会被分配CPU执行时间;
-
Thread.sleep()
执行后,线程就会进入该状态
TERMINATED 终止
表示该线程已经执行完毕;
- 线程Run方法体结束、或主线程(守护线程)的
main()
方法完成时,线程就认为终止了; - 线程对象可能还存活,但不再是单独执行的线程
- 终止的线程不允许再调用
Thread.start()
方法
引用Thread源码
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called {@code Object.wait()}
* on an object is waiting for another thread to call
* {@code Object.notify()} or {@code Object.notifyAll()} on
* that object. A thread that has called {@code Thread.join()}
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
图片转自:Java线程状态分析/线程状态转换图
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。