Java并发编程--线程的生命周期

在 Java 中线程的生命周期中一共有 6 种状态。

1. NEW

Thread state for a thread which has not yet started.
新建状态,线程被创建出来,但尚未启动时的线程状态。

 

2. 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.
就绪状态,表示可以运行的线程状态,它可能正在运行,或者是在排队等待操作系统给它分配 CPU 资源。

 

3. BLOCKED

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 {Object#wait()}.

 

4. WAITING

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()

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

For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object.

A thread that has called Thread.join() is waiting for a specified thread to terminate.

 

5. 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(long)
  • Object#wait(long) with timeout
  • Thread#join(long) with timeout
  • LockSupport#parkNanos
  • LockSupport#parkUntil

6. TERMINATED

The thread has completed execution.

6种状态的流转如下图:

 

posted @ 2020-07-10 18:46  景岳  阅读(117)  评论(0编辑  收藏  举报