Thread Life Cycle In Java

Introduction

In this article we discuss the life cycle of a thread in Java.

Thread

A thread is a single sequential flow of control in a program. Sometimes called a light weight process, so there are 2 different definitions for explaining this topic.

States of threads

A thread life cycle contains the following states:

  1. New State
  2. Runnable State
  3. Running State
  4. Blocked (Non-Runnable) State
  5. Terminated State

pic-1.jpg

1. New Born state

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

pic-2.jpg

2. Runnable state

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();
        //
Our main thread is stopped here,
        //
new thread-0 and new thread-1 are continued
....
      }
    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

pic-3.jpg

3. Running state

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();
        // Our main thread Stopped here,
        // in thread-0 and thread-1 continued..........
      }
    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

pic-4.jpg

4. Blocked (Non-Runnable) State

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:

  1. By calling the "suspend()" method.
  2. By calling the "sleep()" method.
  3. When the "wait()" method is called for synchronization.
  4. 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

pic-5.jpg

5. Dead state

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();


Up Next
    Ebook Download
    View all
    Learn
    View all