As mentioned before, usually you want the main thread to end. In the previous example, this is done by calling sleep () in main (), after a long enough delay to ensure that all child threads end before the main thread. However, this is not a satisfactory solution, it also poses a big problem: How does one thread know that another process has ended? Fortunately, 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:
Final Boolean isAlive ()
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:
final void Join () throws Interruptedexception
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.
1 //Using Join () to-wait for threads to finish.2 classNewthreadImplementsRunnable {3String name;//Name of Thread4 Thread t;5 Newthread (String threadname) {6Name =ThreadName;7t =NewThread ( This, name);8System.out.println ("New Thread:" +t);9T.start ();//Start the threadTen } One //The entry point for thread. A Public voidrun () { - Try { - for(inti = 5; i > 0; i--) { theSYSTEM.OUT.PRINTLN (name + ":" +i); -Thread.Sleep (1000); - } -}Catch(interruptedexception e) { +SYSTEM.OUT.PRINTLN (name + "interrupted.")); - } +SYSTEM.OUT.PRINTLN (name + "exiting.")); A } at } - - classDemojoin { - Public Static voidMain (String args[]) { -Newthread Ob1 =NewNewthread ("One"); -Newthread OB2 =NewNewthread ("the"); inNewthread OB3 =NewNewthread ("three"); -System.out.println ("Thread One is alive:" +ob1.t.isalive ()); toSystem.out.println ("Thread is alive:" +ob2.t.isalive ()); +System.out.println ("Thread Three is alive:" +ob3.t.isalive ()); - //wait for threads to finish the Try { *System.out.println ("Waiting for threads to finish.")); $ Ob1.t.join ();Panax Notoginseng Ob2.t.join (); - Ob3.t.join (); the}Catch(interruptedexception e) { +System.out.println ("Main thread Interrupted"); A } theSystem.out.println ("Thread One is alive:" +ob1.t.isalive ()); +System.out.println ("Thread 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:
New Thread:thread[one,5,main]
New Thread:thread[two,5,main]
New Thread:thread[three,5,main]
Thread one is Alive:true
Thread 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
The exiting.
Three exiting.
One exiting.
Thread one is Alive:false
Thread is Alive:false
Thread Three is Alive:false
Main thread exiting.
As you can see, the call to join () returns after the thread terminates execution.
Series Articles:
Java know how much (top)
Java know how much (interface) interface
Java knows how much (40) the difference between an interface and an abstract class
Java know how much (41) generic explanation
Java know how much (42) the range of generic wildcard characters and type parameters
Java know how much (43) Exception Handling Basics
Java know how much (44) exception type
Java know how much (45) uncaught exceptions
How much Java knows (the) use of try and catch
Java know how much (47) use of multiple catch statements
Java knows how much (in) the nesting of Try statements
Java know how much (a) throw: Exception throws
Java know how many () Java throws clauses
Java knows how many (or) finally
Java know how much (52) built-in exceptions
Java know how much (53) Use Java to create your own exception subclasses
Java know how much (54) assertion detailed
Java know how many (55) threads
Java know how much (56) threading ModelJava know how much (57) main threadJava know how much (58) thread Runnable interface and thread class explanationJava know how much (59) Create multithreading
Java know how much (isAlive) and join () use