简体   繁体   中英

What is the LifeCycle of Thread in Java?

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 ?

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

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM