Example for boolean java.lang.Thread.isAlive()

This blog intended to check the aliveness of a thread. We have used the method boolean isAlive()  define in the class java.lang.Thread, which is not a static method. Aliveness of the thread can be noted in the Thread lifecycle diagram as follows.

 

Threads LifeCycle

I wrote a simple example to demonstrate the status of the aliveness of the Thread. In that program, aliveness tested before start, that is where the thread is in the New state, before end (Runnable, Running, or Blocked) and after end which is Dead status. The AliveExample thread is blocked while main thread is running, although it is alive.

package com.ojitha.thread;

enum Alive {
	YES("  thread is alive"), NO("  thread is not alive");
	private String msg;
	private Alive(String msg){
		this.msg = msg;
	}
	
	public String getMessage(){
		return this.msg;
	}
	
}

public class TestAlive {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		AliveExample a = new AliveExample();
		Thread t = new Thread(a);
		
		//check the status before start the thread
		checkAlive(t, "Before start the thread AliveExample,");
		
		//start the thread
		t.start();
		
		//allow new thread to run at least 10ms
		sleepForWhile(3);
		
		
		//check the status after the thread started
		checkAlive(t, "After start the thread AliveExample,");
		
		//make the AliveExample thread to stop
		a.isRun = false;
		
		
		//Join main thread to end of the AliveExample thread
		//Then main thread will end after the AliveExample thread stop
		System.out.println("In the main Thread AliveExample,");
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("main thread is joined to AliveExample thread...");
		//after stop the thread
		checkAlive(t, "After stop the thread AliveExample,");

	}
	
	public static  void checkAlive (Thread t, String msg){
		if (t.isAlive()){
			System.out.println(msg+Alive.YES.getMessage());
		} else System.out.println(msg+Alive.NO.getMessage());
	}
	
	public static void sleepForWhile(int timeToWait){
		try {
			Thread.sleep(timeToWait);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}

class AliveExample implements Runnable{
	boolean isRun = true;
	
	public void run (){
		while(isRun){
			System.out.println("Running in the AliveExample thread....");
		}
	}
}

Due to the high uncertainty behavior of the thread, main thread has joined to the AliveExample thread before check the aliveness.

If you are really interesting, you can follow the following output generated from the above program.

Before start the thread AliveExample,  thread is not alive
Running in the AliveExample thread....
Running in the AliveExample thread....
.................................
.................................
.................................
Running in the AliveExample thread....
Running in the AliveExample thread....
After start the thread AliveExample,  thread is alive
In the main Thread AliveExample,
Running in the AliveExample thread....
main thread is joined to AliveExample thread...
After stop the thread AliveExample,  thread is not alive

As a conclusion, Thread being alive after it entered to the Runnable state till exit from the Running state.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python