Home Tutorial Java Core Java Thread : isAlive() method

 
 

Java Thread : isAlive() method
Posted on: October 18, 2012 at 12:00 AM
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

Related Tags for Java Thread : isAlive() method :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.