Java Thread : isAlive() method


 

Java Thread : isAlive() method

In this tutorial you will learn how to use isAlive() method in java thread.

In this tutorial you will learn how to use isAlive() method in java thread.

Java Thread : isAlive() method

In this tutorial you will learn how to use isAlive() method in java thread.

isAlive() method :

When you are running many threads ,java take long periods switching between threads , may be one of your thread dead. This can be checked by using method Thread.isAlive().

boolean isAlive() - It is of Boolean type. It returns true if your thread is alive means your thread is started and not yet died, otherwise it returns false.

Example :  In this example we are checking thread is alive or not.

class ThreadIsAlive implements Runnable {

	@Override
	public void run() {
		Thread thread = Thread.currentThread();

		System.out.println("Is Thread alive ? - " + thread.isAlive());

	}

	public static void main(String args[]) throws Exception {
		Thread thread = new Thread(new ThreadIsAlive());
		thread.start();
		thread.join();

		System.out.println("Is Thread alive ? - " + thread.isAlive());
	}
}

Output :

Is Thread alive ? - true
Is Thread alive ? - false

Ads