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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36425942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:52:16  来源:igfitidea点击:

What is the LifeCycle of Thread in Java?

javamultithreadinglifecycle

提问by Ullas

In java when we create an object of thread

在java中,当我们创建一个线程对象时

Thread t1 = new Thread(Runnable object);
t1.start();

What are the different stages of lifecycle of thread t1and after execution of run()will be the state of t1?

线程生命周期的不同阶段t1和执行run()后将是t1什么状态?

回答by user6160675

Thread States:

线程状态:

  1. New - Created but not executed
  2. Runnable - Running
  3. Terminated - End of the Run Method Scope was reached.
  1. 新建 - 创建但未执行
  2. 可运行 - 运行
  3. 已终止 - 已达到运行方法范围的结尾。

A Thread can also have Waiting, Timed Waiting and Blocked as Status

一个线程也可以有 Waiting、Timed Waiting 和 Blocked 作为状态

For Further Informations see here: https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html

有关更多信息,请参见此处:https: //docs.oracle.com/javase/7/docs/api/java/lang/Thread.State.html

回答by Venkat

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.

一个线程在其生命周期中经历了不同的阶段。例如,一个线程诞生、启动、运行,然后死亡。下图展示了一个线程的完整生命周期。

enter image description here

在此处输入图片说明

Java Thread Above-mentioned stages are explained here:

Java Thread 上面提到的阶段在这里解释:

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.

New:新线程在新状态下开始其生命周期。它一直保持这种状态,直到程序启动线程。它也被称为天生线程。

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

Runnable: 新线程启动后,线程变为可运行状态。处于此状态的线程被认为正在执行其任务。

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.

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.

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

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

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

Source:http://www.tutorialspoint.com/java/java_multithreading.htm

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

回答by Ravindra babu

A thread can be in one of the following State:

线程可以处于以下状态之一

NEW :A thread that has not yet started is in this state.

NEW :尚未启动的线程处于此状态。

RUNNABLE :A thread executing in the Java virtual machine is in this state.

RUNNABLE :在 Java 虚拟机中执行的线程处于这种状态。

BLOCKED:A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronizedblock/method after calling Object.wait.

BLOCKED:处于阻塞状态的线程正在等待监视器锁进入同步块/方法或synchronized调用后重新进入块/方法Object.wait

WAITING:A thread that is waiting indefinitely for another thread to perform a particular action is in this state.

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

TIMED_WAITING:A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.

TIMED_WAITING:等待另一个线程执行操作达指定等待时间的线程处于此状态。

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:A thread that has exited is in this state.

TERMINATED:已退出的线程处于此状态。

Refer to this articleby pramodbablad to understand various states in this diagram

请参阅pramodbablad 的这篇文章以了解此图中的各种状态

enter image description here

在此处输入图片说明

In above diagram, except RUNNING( which has been shown in a circle), all other rectangular blocks denotes various thread states.

在上图中,除了RUNNING(已用圆圈表示)之外,所有其他矩形块都表示各种thread states