There are two ways to determine whether a thread has finished or not.
- isAlive()
- join()
isAlive(): isAlive() method is used to check whether a thread is in the running or not .
or
In a another word, this method determines thread is live or dead.
isAlive( )method returns true if the thread is still running otherwise it returns false.
join(): This method wait for the specified thread to terminate.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import java.lang.*; public class Demo implements Runnable { public void run() { Thread t = Thread.currentThread(); // tests if this thread is alive System.out.println("thread is live = " + t.isAlive()); // tests if this thread is alive } public static void main(String args[]) throws Exception { Thread t = new Thread(new Demo()); t.start(); // this will call run() function t.join(); // waits for this thread to die System.out.println("thread is live = " + t.isAlive()); // tests if this thread is alive } } |
Result
1 2 | thread is live = true thread is live = false |