What is the LifeCycle of Thread in Java?

亡梦爱人 提交于 2019-12-10 12:13:09

问题


In java when we create an object of thread

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

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


回答1:


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 Thread Above-mentioned stages are explained here:

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: After a newly born thread is started, the thread becomes runnable. A thread in this state is considered to be executing its task.

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: 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 ): A runnable thread enters the terminated state when it completes its task or otherwise terminates.

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




回答2:


A thread can be in one of the following State:

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

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

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 that is waiting indefinitely for another thread to perform a particular action is in this state.

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.

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.

Refer to this article by pramodbablad to understand various states in this diagram

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




回答3:


Thread States:

  1. New - Created but not executed
  2. Runnable - Running
  3. Terminated - End of the Run Method Scope was reached.

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

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



来源:https://stackoverflow.com/questions/36425942/what-is-the-lifecycle-of-thread-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!