Overview
In this chapter, the join () method in thread is described. The topics covered include:
1. Join () Introduction
2. Join () source analysis (based on jdk1.7.0_40)
3. Join () example
Reprint Please specify source:http://www.cnblogs.com/skywang12345/p/3479275.html
1. Join () Introduction
Join () is defined in Thread.java.
Join (): Lets the main thread wait for the "child threads" to end before they can continue to run. This sentence may be a bit obscure, we still use examples to understand:
main thread extendsvoidnew Son (); S.start (); S.join (); // sub -thread extendsvoid run () {...}}
Description :
There are two classes father (the main thread class) and son (child threading Class). Because son is created and started in father, Father is the main thread class and son is the child thread class.
In the father main thread, create a new child thread s through New Son (). The "Child thread S" is then started through S.start (), and S.join () is called. After calling S.join (), the Father main thread waits until the "child thread S" is finished, and after the "Child thread S" has finished running, the father main thread can then run. This is what we call the "join" function, which is to allow the main thread to wait for the child thread to end before it can continue to run!
2. Join () source analysis (based on jdk1.7.0_40)
PublicFinalvoid Join ()Throwsinterruptedexception {Join (0);}PublicFinalSynchronizedvoid Join (LongMillis)Throwsinterruptedexception {Long base =System.currenttimemillis ();Long now = 0;if (Millis < 0) {throw new illegalargumentexception ("Timeout value is Negative "); } if (Millis = = 0while (IsAlive ()) {Wait (0else {while (IsAlive ()) {long delay = Millis- now; if (Delay <= 0break base;}}
Description :
From the code, we can see. When millis==0, it enters the while (IsAlive ()) loop, i.e. the main thread waits as long as the child thread is alive.
We understand the use of join () based on the code above explaining the function of join ()!
Questions :
Although S.join () is called in the "Father Main Thread", S.join () is the join () called by "Child thread S". Then, IsAlive () in the Join () method should be judged as "child thread S" is not alive state; the corresponding wait (0) should also be "let child thread S" wait. But if this is the case, how can s.join () be "Let the main thread wait until the child thread S is finished", should be let "the child thread waits for the right (because call the child thread object s's Wait method)"?
answer : The Role of Wait () is to have the "current thread" wait, and here the "current thread" refers to the thread that is currently running on the CPU. So, although it is called the Wait () method of the child thread, it is called through the "main thread", so the main thread is dormant, not "child thread"!
3. Join () Example
After understanding the role of join (), follow the example to see the use of join ().
//Source Code of Jointest.javaPublicClassjointest{PublicStaticvoidMain (string[] args) {Try{Threada T1 =New Threada ("T1");//New "Thread T1"T1.start ();//Start "Thread T1" T1.join ();//"Thread T1" is added to the main thread main, and "main thread main () waits for its completion" System.out.printf ("%s finish\n", Thread.CurrentThread (). GetName ()); }Catch(Interruptedexception e) {E.printstacktrace ();} }static class threada Extends thread{publicsuperpublic void run () {System.out.printf ("%s start\n", Span style= "color: #0000ff;" >this.getname ()); // deferred operation for (int i=0; I <1000000; I++); System.out.printf ("%s finish\n", this.getname ());}}}
Operation result :
T1 STARTT1 Finishmain Finish
Result Description :
Run the process
(01) Create a new thread T1 through new Threada ("T1") in main thread main. Next, start thread T1 with T1.start () and execute T1.join ().
(02) after executing t1.join (), "main thread main" will enter "blocking state" to wait for T1 to finish running. After the "Child thread T1" ends, it wakes up "main thread main", "Main thread" regain CPU execution and continue running.
Java Multithreading Series--"Basic" 08 join ()