且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

什么是Java线程的生命周期?

更新时间:2023-01-25 17:42:31

线程在其生命周期中经历了多个阶段.例如,线程是先生,启动,运行然后死亡的.下图显示了线程的完整生命周期.

A thread goes through various stages in its life cycle. For example, a thread is born, started, runs, and then dies. Following diagram shows the complete life cycle of a thread.

Java线程 上述步骤在这里进行了解释:

Java Thread Above-mentioned stages are explained here:

New:新线程以新状态开始其生命周期.它将保持这种状态,直到程序启动线程为止.也称为出生线程.

New: A new thread begins its life cycle in the new state. It remains in this state until the program starts the thread. It is also referred to as a born thread.

Runnable:启动新出生的线程后,该线程变为可运行的.处于这种状态的线程被视为正在执行其任务.

Runnable: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

Waiting:有时,一个线程在等待另一个线程执行任务时会转换为等待状态,仅当另一个线程发出信号等待该线程继续执行时,该线程才转换回可运行状态.

Waiting: Sometimes, a thread transitions to the waiting state while the thread waits for another thread to perform a task.A thread transitions back to the runnable state only when another thread signals the waiting thread to continue executing.

Timed waiting:可运行线程可以在指定的时间间隔内进入定时等待状态.当该时间间隔到期或发生等待事件时,处于此状态的线程将转换回可运行状态.

Timed waiting: A runnable thread can enter the timed waiting state for a specified interval of time. A thread in this state transitions back to the runnable state when that time interval expires or when the event it is waiting for occurs.

Terminated ( Dead ):可运行线程在完成其任务或以其他方式终止时进入终止状态.

Terminated ( Dead ): A runnable thread enters the terminated state when it completes its task or otherwise terminates.

来源: http://www.tutorialspoint.com/java/java_multithreading.htm