How does one thread know the other is over? The thread class provides a way to answer this question.
There are two ways to determine whether a thread is ending. First, IsAlive () can be called in the thread. This method is defined by the thread, and its usual form is as follows:
?
If the calling thread is still running, the IsAlive () method returns True if it is not, and returns false. But IsAlive () is seldom used, and the more common way to wait for a thread to end is to call join (), which is described as follows:
?
| 1 |
finalvoid join( ) throwsInterruptedException |
The method waits for the calling thread to end. The name comes from the notion that the thread is required to wait until the specified thread participates. The additional form of join () allows you to define a maximum time to wait for the specified thread to end. The following is an improved version of the previous example. Use join () to ensure that the main thread ends at the end. Again, it demonstrates the IsAlive () method.
?
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
// Using join() to wait for threads to finish.class NewThread implements Runnable { String name; // name of thread Thread t; NewThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New thread: " + t); t.start(); // Start the thread } // This is the entry point for thread. public void run() { try { for(int i = 5; i > 0; i--) { System.out.println(name + ": " + i); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println(name + " interrupted."); } System.out.println(name + " exiting."); }}class DemoJoin { public static void main(String args[]) { NewThread ob1 = new NewThread("One"); NewThread ob2 = new NewThread("Two"); NewThread ob3 = new NewThread("Three"); System.out.println("Thread One is alive: "+ ob1.t.isAlive()); System.out.println("Thread Two is alive: "+ ob2.t.isAlive()); System.out.println("Thread Three is alive: "+ ob3.t.isAlive()); // wait for threads to finish try { System.out.println("Waiting for threads to finish."); ob1.t.join(); ob2.t.join(); ob3.t.join(); } catch (InterruptedException e) { System.out.println("Main thread Interrupted"); } System.out.println("Thread One is alive: "+ ob1.t.isAlive()); System.out.println("Thread Two is alive: "+ ob2.t.isAlive()); System.out.println("Thread Three is alive: "+ ob3.t.isAlive()); System.out.println("Main thread exiting."); }} |
The program output is as follows:
?
| 1234567891011121314151617181920212223242526272829 |
New thread: Thread[One,5,main]New thread: Thread[Two,5,main]New thread: Thread[Three,5,main]Thread One is alive: trueThread Two is alive: trueThread Three is alive: trueWaiting for threads to finish.One: 5Two: 5Three: 5One: 4Two: 4Three: 4One: 3Two: 3Three: 3One: 2Two: 2Three: 2One: 1Two: 1Three: 1Two exiting.Three exiting.One exiting.Thread One is alive: falseThread Two is alive: falseThread Three is alive: falseMain thread exiting. |
As you can see, the call to join () returns after the thread terminates execution.
The use of IsAlive () and join () in Java threading programming