Java isAlive () and join () use

As mentioned earlier, you usually want the main thread finally ended. In the previous example, this is done by calling sleep (in main (),) to achieve, after a delay long enough to ensure that all child threads before the end of the main thread. However, this is not a satisfactory solution, it also brings a big question: How do I know one thread to another thread has ended? Fortunately, Thread class provides the answer to this problem.

There are two ways to determine whether a thread ends. First, you can call isAlive () in the thread. This method is defined by Thread, its general form as follows:
Final Boolean isAlive ()

If the calling thread is still running, isAlive () method returns true, if not false is returned. But isAlive () is rarely used, the more common method is to call the end of the waiting thread join (), described as follows:
Final void the Join () throws InterruptedException

This method waits for the end of the calling thread. The name comes from the requirements concept thread to wait until the specified thread engagement. join () allows an additional form to wait for the end of the specified thread define a maximum time. Here is an improved version of the previous example. Use join () to ensure that the main thread finally ended. Similarly, it also demonstrates the isAlive () method.
The Using the Join // () to the wait for Threads to Finish.
Class newthread the implements the Runnable {
String name; // Thread name of
the Thread T;
newthread (String ThreadName) {
name = ThreadName;
T = the Thread new new (the this, name);
the System .out.println ( "New Thread:" + T);
t.start (); // The Thread the Start
}
// This IS Point The entry for Thread.
public void RUN () {
the try {
for (int I =. 5 ; I> 0; I-) {
System.out.println (name + ":" + I);
the Thread.sleep (1000);
}
} the 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.”);
}
}

程序输出如下所示:
New thread: Thread[One,5,main] New thread: Thread[Two,5,main] New thread: Thread[Three,5,main] Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.

As you can see, calling join () returns, the thread terminated.

1. The concept of threads
2. the Java threading model
3. The main thread
4. Create a thread
5. Create a multithreaded
6. Use isAlive () and join () of
7. thread priority
8. Thread Synchronization
9. The inter-thread communication
10. thread deadlock
11. the thread suspend, resume and terminate

Guess you like

Origin blog.csdn.net/Javaxuxuexi/article/details/92716537