线程的生命周期

1.线程状态介绍

NEW

Thread state for a thread which has not yet started.

新建:已经创建线程,但还没有启动

RUNNABLE

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.

可运行:包括就绪状态和运行状态

BLOCKED

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 Object.wait.

阻塞:等待获取锁

WAITING

A thread in the waiting state is waiting for another thread to perform a particular action.

等待:调用以下方法时处于等待状态

Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:

  • Object.wait with no timeout
  • Thread.join with no timeout
  • LockSupport.park

TIMED_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:

  • Thread.sleep
  • Object.wait with timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil

TERMINATED

Thread state for a terminated thread. The thread has completed execution.

终止:线程执行完毕。

2.状态之间的转换

线程的生命周期