Home Tutorial Java Core Java Thread : isAlive() method

 
 

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

Related Tags for Java Thread : isAlive() method :


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.

Posted on: October 18, 2012

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com