Share on Google+Share on Google+

Java Thread : isAlive() method

Advertisement
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

Advertisements

Posted on: October 18, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.