07_ control thread _join_ thread queue

Source: Internet
Author: User

"Join Thread Brief"

Join () Method: thread provides for one thread to wait for another thread to finish. When a Join method (T2.join ()) is called on another thread (such as a T2 thread ) in a program execution flow (such as a main thread ), the calling thread (main thread ) is blocked. Until the join thread (T2.start ()) joined by the join () method is finished.

"Sample Code"

Package com. higgin.part02;class  jointhread  implements Runnable{//override the Run method to define the thread execution body     Public voidrun () { for(intI=0;i< -; i++) {System. out. println (Thread.CurrentThread (). GetName () +"===="+i); }    }} Public classDemo01 { Public Static voidMain (string[] args) throws interruptedexception {  jointhread JT=New jointhread(); Thread T1=NewThread (JT,"Higgin"); Thread T2=NewThread (JT,"Threads to be join");   T1.start (); //Start T1 thread //The following is the main main thread         for(intI=0;i< -; i++){            if(i== -) {T2.start (); //Start t2 thread The //main thread calls the join method of the T2 thread, causing the main thread to wait for the T2 execution to finish before it can execute downT2.join (); } System. out. println (Thread.CurrentThread (). GetName () +"===="+i); }    }}

"Run Results"

At first, the main thread initiated the T1 thread called "Higgin", at which time a total of two threads were executed concurrently, the main thread and the Higgin thread, respectively.

Later in the For loop in main main thread, when i==20, the T2 thread was started, and the method of T2 thread's join was called, causing the main thread to block directly.

Because the main thread is already blocked, the next step is to preempt both "Higgin" and "Joined Threads", and if the "Join thread" ends, the main thread goes into a ready state and then preempted, as if the Higgin thread is not finished.

Added

Join () method has three kinds of overloaded forms;

1. Join (): That is, the thread that waits for the join () completes

2. Join (Long Millis): The maximum time to wait for a thread to be join () is Millis milliseconds, and if the time is up, the thread of the join is still not finished, then it will not wait.

3. Join (Long Millis,int Nanos): Added a nanosecond, waiting for the thread to be join () for a maximum time of millis milliseconds and Nanos nanoseconds (rarely used, usually accurate to milliseconds)

07_ control thread _join_ thread queue

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.