This chapter covers the following topics:
1. Join () Introduction
2. Join () source analysis (based on jdk1.7.0_40)
3. Join () example
1. Join () Introduction
Join () is defined in Thread.java.
Join () Function: Let the main thread wait for the child thread to finish before it can continue to run. This sentence may be a bit obscure, we still use the example to understand:
Copy Code code as follows:
Main thread
public class Father extends Thread {
public void Run () {
Son s = new Son ();
S.start ();
S.join ();
...
}
}
Child threads
public class Son extends Thread {
public void Run () {
...
}
}
Description
There are two class father (main thread classes) and son (child thread classes) above. Because son is created and started in father, Father is the main thread class, and son is a child thread class.
In the father main thread, new "child thread S" is created through the new Son (). Then start "Child thread S" via S.start () and Invoke S.join (). After the call to S.join (), the Father main thread waits until "child thread S" is finished, and the father main thread runs after the thread s runs. This is what we call "join (), which allows the main thread to wait for the child to end before it can continue to run!"
2. Join () source analysis (based on jdk1.7.0_40)
Copy Code code as follows:
Public final void Join () throws Interruptedexception {
Join (0);
}
Public final synchronized void join (long Millis)
Throws Interruptedexception {
Long base = System.currenttimemillis ();
Long now = 0;
if (Millis < 0) {
throw new IllegalArgumentException ("Timeout value is negative");
}
if (Millis = = 0) {
while (IsAlive ()) {
Wait (0);
}
} else {
while (IsAlive ()) {
Long delay = Millis-now;
if (delay <= 0) {
Break
}
Wait (delay);
now = System.currenttimemillis ()-base;
}
}
}
Description
From the code, we can see that. When millis==0, it enters the while (IsAlive ()) loop, which means that as long as the child thread is alive, the main thread waits continuously.
We understand the use of join () based on the code that explains the join () function.
Problem:
Although the S.join () is invoked in the "Father Main Thread", S.join () is the join () that is invoked through "child thread S". Then, the IsAlive () in the Join () method should be to determine if "child thread S" is alive State, and the corresponding wait (0) should also be "let child thread S" waiting. But if so, how can the role of S.join () be "Let the main thread wait until the child thread s completes", should be to let "child thread waiting for the" (because of the call child thread object s wait method)?
The answer: Wait () is to keep the current thread waiting, and here "current thread" refers to the thread currently running on the CPU. So, although it is called the Wait () method of the child thread, it is invoked through the "main thread", so the main thread is dormant, not the child thread!
3. Join () example
After understanding the role of join (), let's look at the use of join () by example.
Copy Code code as follows:
Jointest.java's source code
public class jointest{
public static void Main (string[] args) {
try {
Threada T1 = new Threada ("T1"); New "Thread T1"
T1.start (); Start the thread T1
T1.join (); Add thread t1 to main thread main, and the main thread main () waits for it to complete.
System.out.printf ("%s finish\n", Thread.CurrentThread (). GetName ());
catch (Interruptedexception e) {
E.printstacktrace ();
}
}
Static class Threada extends thread{
Public Threada (String name) {
Super (name);
}
public void Run () {
System.out.printf ("%s start\n", This.getname ());
Delay operation
for (int i=0 i <1000000; i++)
;
System.out.printf ("%s finish\n", This.getname ());
}
}
}
Run Result:
Copy Code code as follows:
T1 start
T1 finish
Main finish
Results show:
Run the process as shown
(01) New "Thread T1" in "Main thread main" through New Threada ("T1"). Next, start thread T1 with T1.start () and execute T1.join ().
(02) after executing t1.join (), "main thread main" enters "blocking state" and waits for T1 to run. After the child thread T1 is finished, the main thread main is awakened, and the main thread retrieves the CPU execution power and continues to run.