Java multithreaded--join functions

Source: Internet
Author: User

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 public class Father extends thread {public void run () {Son s = new Son ();        S.start ();        S.join (); ...    }} Child thread public class son extends thread {public void run () {...}} <span style= "font-family: ' Courier New '!important; FONT-SIZE:12PX!important; line-height:1.5!important; Color:rgb (0, 0, 0); " ></span> 

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)
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; }}}<span style= "font-family: ' Courier New '!important; FONT-SIZE:12PX!important; line-height:1.5!important; Color:rgb (0, 0, 0); " ></span> 

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 (), the following example looks at the use of join ().

Jointest.java source public class jointest{public static void Main (string[] args) {try {Threada T1 = new Threada ("T1");                     New "Thread T1" T1.start ();                        Start "Thread T1" T1.join (); Add "Thread T1" to the main thread main, and "main thread main () will wait for its completion" 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 ()); }}}<span style= "font-family: ' Courier New '!important; FONT-SIZE:12PX!important; line-height:1.5!important; Color:rgb (0, 0, 0); " ></span>

Operation result :

T1 startt1 finishmain finish<span style= "font-family: ' Courier New '!important; FONT-SIZE:12PX!important; line-height:1.5!important; Color:rgb (0, 0, 0); " ></span>

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 multithreaded--join functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.