Advanced Java Tutorial


Total available pages count: 55
Subject - Java Technologies

Life cycle of thread

The thread works through various stages of its life cycle.java virtual machine used to control the life cycle of the thread.

Life_Cycle_Of_Thread

A Thread is one of five states. According to the sun info system, the life cycle of the thread had 5 states.

New

In New state, the thread creates an instance of thread class before the invocation of the start() method. It is also mentioned as a born thread.

Runnable runnable = new NEWState();
Thread t = new Thread(runnable);
Log.info(t.getState());

 

Runnable

After calling the start() method (to start the thread), the thread is in a runnable state. the runnable state also recognized to be executing its task. then the thread scheduler needs to select it to be the running thread.

Runnable runnable = new NEWState();
Thread t = new Thread(runnable);
t.state();
Log.info(t.getState());

 

Running

In the running state, the thread is either running or be ready for execution.

Blocked

In this state, the thread still is alive or currently not eligible to run. When it waits for the monitor lock & tries to access the section of code that blocked by another thread.

public class BlockedState {
	public static void main(String[] args) throws InterruptedException {
		Thread t1 = new Thread(new DemoThreadB());
		Thread t2 = new Thread(new DemoThreadB());
		
		t1.start();
		t2.start();
		Thread.sleep(1000);
		Log.info(t2.getState());
		System.exit(0);
	}
}
class DemoThreadB implements Runnable {
	@Override
	public void run() {
		commonReasource();
	}
public static synchronized void commonReasource() {
	while(ture) {
	   // Infinite loop to mimic heavy processing
	  // 't1' won't leave this method
	  // when 't2' try to enter this
	 }
     }
}

 

Waiting

In the thread transition, waiting for the state while the thread does wait for another thread to invoke task. The thread back to a runnable state only if another thread gives a signal to the waiting thread to continue execution.

public class WaitingState implements Runnable  {
	public static Thread t1;

	public state void main(String[] args) {
	t1 = new Thread(new WaitingState());
	t1.Start();

	try {
	  t2.join();
	} catch(InterruptedExceptione) {
		Thread.currentThread().interrupt();
		Log.error("Thread interrupted", e);
		}
	}
}

class DEMOThreadWS implements Runnable {
	public void run() {
	 try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {
	   Thread.currentThread().interrupt();
	   Log.error("Thread interrupted", e);
	}
	Log.info(WaitingState.t1.getState());
}
}

 

Dead (Terminated)

The thread is in a Dead or terminated state when it exits the run() method. The runnable state enters into a terminated state when it completes the task execution.

public class TerminatedState implements Runnable {
	public static void main(string[] args) throws InterruptedException {
	Thread t1= new Thread(new TerminatedState());
	t1.star();
	// The following sleep method will give enough time for 
	//thread t1 to complete
	Thread.sleep(1000);
	Log.info(t1.getState());
	}
	@Override
	public void run() {
	// No processing in the block
	}
	}

 

Multithreading is obtained by the concept of thread.

MultiThreading_Concept_With_Thread


Comments