Thread States in Java
A thread is a program in execution created to perform a specific task. Life cycle of a Java thread starts with its birth and ends on its death.
The start() method of the Thread class is used to initiate the execution of a thread and it goes into runnable state and the sleep() and wait() methods of the Thread class sends the thread into non runnable state.
After non runnable state, thread again comes into runnable state and starts its execution. The run() method of thread is very much important. After executing the run() method, the lifecycle of thread is completed.
All these phases of threads are the states of thread in Java.
To work with threads in a program, it is important to identify thread state. The following figure shows thread states in Java thread life cycle.
Thread States in Java
A thread is a path of execution in a program that goes through the following states of a thread. The five states are as follows:
New (Newborn State)
When an instance of the Thread class is created a new thread is born and is known to be in New-born state. That is, when a thread is born, it enters into new state but its execution phase has not been started yet on the instance.
In simpler terms, Thread object is created but it cannot execute any program statement because it is not in an execution state of the thread. Only start() method can be called on a new thread; otherwise, an IllegalThreadStateException will be thrown.
The second phase of a new-born thread is the execution phase. When the start() method is called on a the new instance of a thread, it enters into a runnable state.
If all threads have equal priority, a time slot is assigned for each thread execution on the basis of first-come, first-serve manner by CPU. The process of allocating time to threads is known as time slicing. A thread can come into runnable state from running, waiting, or new states.
Running means Processor (CPU) has allocated time slot to thread for its execution. When thread scheduler selects a thread from the runnable state for execution, it goes into running state. Look at the above figure.
In running state, processor gives its time to the thread for execution and executes its run method. It is the state where thread performs its actual functions. A thread can come into running state only from runnable state.
A running thread may give up its control in any one of the following situations and can enter into the blocked state.
A thread is considered to be in the blocked state when it is suspended, sleeping, or waiting for some time in order to satisfy some condition.
A thread dies or moves into dead state automatically when its run() method completes the execution of statements. That is, a thread is terminated or dead when a thread comes out of run() method. A thread can also be dead when the stop() method is called.
During the life cycle of thread in Java, a thread moves from one state to another state in a variety of ways. This is because in multithreading environment, when multiple threads are executing, only one thread can use CPU at a time.
All other threads live in some other states, either waiting for their turn on CPU or waiting for satisfying some conditions. Therefore, a thread is always in any of the five states.
Java Thread Program
Thread 1 i in Thread 1 i = 1 Main Thread End Thread 2 i in Thread 2 i = 1 i = 2 i = 3 i = 4 i = 5 Thread 2 Completed. i = 2 i = 3 i = 4 i = 5 Thread 1 Completed.
In the above program, Java threads are implemented by creating instances of Thread class, t1 and t2. Different methods such as start(), yield(), run(), sleep() of Thread class are implemented.