Introduction
In this article, we discuss the life cycle of a thread in Java.
What is Thread
A thread is a single sequential flow of control in a program. Sometimes called a lightweight process, so there are 2 different definitions for explaining this topic.
States of threads
A thread life cycle contains the following states:
- New State
- Runnable State
- Running State
- Blocked (Non-Runnable) State
- Terminated State
1. Initializing Thread
When we start a thread it goes into the new state.
In this state, the thread is only created but not working.
We can move the thread to the running state by invoking the "start()" method & the thread can be killed by the "stop()" method.
Syntax
Thread thread = new Thread;
Example
In this example we create a simple thread; we use the "sleep()" method for delaying the thread for execution.
- public class ThreadEx {
- public static void main(String args[]) throws Throwable {
- new ThreadEx().threadfunctn();
- }
- void threadfunctn() throws Throwable {
- for (int i = 0; i < 10; i++) {
- System.out.println("thread in " + Thread.currentThread().getName() + " process " + i);
- Thread.sleep(500);
- }
- }
- }
Output
2. Runnable state of Thread
In this state Thread is in the running mode & waiting for control input.
We can move control to another thread by the "yield()" method.
Example
In this example we create two parallel threads using the "runnable()" interface, in other words using a "run()" method.
- public class RunnableEx implements Runnable {
- public static void main(String args[]) throws Throwable {
- RunnableEx run1 = new RunnableEx();
- RunnableEx run2 = new RunnableEx();
- new Thread(run1).start();
- new Thread(run2).start();
-
-
- }
- public void run() {
- try {
- for (int i = 0; i < 10; i++) {
- System.out.println("In thread " + Thread.currentThread().getName() + " processed " + i);
- Thread.sleep(500);
- }
- } catch (Throwable tr) {}
- }
- }
Output
3. Running state of Thread
In this state the thread is ready for execution since the control of the CPU is provided to that thread.
Syntax
thread.start()
Example
- public class RunEx extends Thread {
- public static void main(String args[]) throws Throwable {
- new RunEx().start();
- new RunEx().start();
-
-
- }
- public void run() {
- try {
- for (int i = 0; i < 5; i++) {
- System.out.println(" In thread " + Thread.currentThread().getName() + " process " + i);
- Thread.sleep(520);
- }
- } catch (Throwable tr) {}
- }
- }
Output
4. Blocked (Non-Runnable) State of Thread
When our thread is not permitted to enter the Running and Runnable states, then we say it is in the blocked state.
The various ways a thread can go into a blocked state are:
- By calling the "suspend()" method.
- By calling the "sleep()" method.
- When the "wait()" method is called for synchronization.
- When an I/O operation is performed in our program, the thread is implicitly blocked by the JVM.
Example
- class BlockedEx extends Thread {
- public void run() {
- for (int i = 1; i < 5; i++) {
- try {
- Thread.sleep(4500);
- } catch (InterruptedException eb) {
- System.out.println(eb);
- }
- System.out.println(i);
- }
- }
- public static void main(String args[]) {
- BlockedEx t1 = new BlockedEx();
- BlockedEx t2 = new BlockedEx();
- t1.start();
- t2.start();
- }
- }
Output
5. Dead state of Thread
When the process of the "run()" method of the thread is completed, the process of the particular thread is completed, in other words ended.
We can kill that particular thread by using the "stop()" method; for that particular thread and move it to be in the dead state.
Here is the example of a dead state:
- BlockedEx t1=new BlockedEx();
- t1.stop();