Java thread's Join method.

Source: Internet
Author: User

1, the function of the method:

The parent thread waits for the child thread to execute.

If it is a join or join (0), that is, the wait time is 0, the parent thread waits until the child thread finishes executing.

If it is join (time), that is, the wait length is the time value, the parent thread waits longer depending on the situation:

The first: the length of the child thread execution does not require time, so the parent thread may not wait for the time duration, why is it possible? Whether to consider other threads (excluding parent threads, and threads other than child threads), he may also let the parent thread wait (no reason to get to the CPU time slice, etc.),

Second: The child thread executes longer than time, and the parent thread waits longer, at least time,

2, Source analysis:

1   PublicFinal synchronizedvoidJoinLongMillis)2 throws Interruptedexception {3         Long Base=System.currenttimemillis ();4         Longnow =0;5 6         if(Millis <0) {7             Throw NewIllegalArgumentException ("timeout value is negative");8         }9 Ten         if(Millis = =0) { One              while(IsAlive ()) { AWait0); -             } -}Else { the              while(IsAlive ()) { -                 LongDelay = Millis-Now ; -                 if(Delay <=0) { -                      Break; +                 } - wait (delay); +now = System.currenttimemillis ()-Base; A             } at         } -}

  

1. first determine that the join method must have a parameter that is greater than 0, otherwise the method throws an exception illegalargumentexception ("Timeout value is negative");

   2. Join (0), enter the loop :

while (IsAlive ()) {

    Wait (0);


     
So what does IsAlive mean? IsAlive refers to the child thread start and the Run method is not finished, the child thread is alive, so if you call the Join method before the child thread start, it has no effect, at which point the child thread is dead.
  3.join (Time), enter the loop:
          while (IsAlive ()) {
Long delay = Millis-now ; if (delay <= 0) { break Wait (delay); now = System.currenttimemillis ()-Base }
Code meaning:

The first: the length of the child thread execution does not require time, then the parent thread may not wait for the time duration, the loop condition IsAlive () =false, the parent thread is out

The second: The child thread executes longer than time, then the parent thread waits for a length of time, at least time, and the parent threads break out.

Summary :

The only thing you have to remember is that the parent thread is sure to wait for the child thread to execute for a while, and what is the time? To differentiate,

Join (0): it must wait until the child thread finishes executing, and the parent thread executes;

When join (time): The parent thread executes until the child thread finishes executing or at least the time is long.

Java thread's Join method.

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.