Java Thread Collaboration Join ()

Source: Internet
Author: User

In actual development we tend to encounter situations where a thread's execution needs to depend on the results of another thread's execution. That is, the main thread generated and started the child thread, if the sub-thread to do a lot of time-consuming operations, the main thread will often end before the child thread, but if the main thread finishes processing other transactions, it is necessary to use the child thread processing results, that is, the main thread needs to wait for the sub-thread to complete before the end The join () method will be used at this time.

Join ()

The join () method is primarily for the thread that invokes the method to complete the task in the Run method, and then executes the generation following the join () method.

The main thread generates and starts the sub-thread, and the sub-thread takes a lot of time-consuming operations (which can be borrowed from the thread), and when the main thread finishes processing the other transactions, it needs to use the result of the sub-threading, and this time it will use the join () method.

The method waits indefinitely, blocking the current thread until the target thread (the thread that called the method) finishes executing. Join () 0

The method needs to give the maximum wait time, and if the target thread executes more than a given time, then the front-end will not execute the public final synchronized void join (long Millis) without waiting for the target thread to execute. Throws interruptedexception{}

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

String str= = "Enter" +thread.currentthread (). GetName () + "thread" 1000= "Hello Word" + "thread Business Processing Complete" SYSTEM.OUT.PRINTLN (threa D.currentthread (). GetName () + "threading Business Start" "Get str value:" +

Main thread processing business started
Enter Thread-0 thread
Get str value: null
Thread-0 Thread Business Processing complete

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

Running the code, it seems that the value of STR is never seen as "Hello Word", and each time is null, because it is very simple, because in the thread of the Run method is assigned to STR before the operation of 1 seconds sleep, At this point the System.out.println method in the main thread has been executed, so it is difficult to see that the value of str is "Hello word", in order to see the value of str is "Hello word", one of our ideas is to wait for the thread to run after the end, We can do the System.out.println again, when the Join method is shown, we delete the comment code above, and then run, no matter how many times, the output is "Hello Word", From this example, we can see the role of the Join method, which adjusts the order of operations between the various threads, allowing synchronization to be achieved.

If the code that modifies the comment is Thread.Join (100), the result is: Get the str value: null, because the main thread can't wait.

Join () He lets the calling thread wait on the current thread object, and when the thread finishes executing, the waiting thread calls the Notifyall () method to notify all the waiting threads to continue execution before it is rolled out.

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>

    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;             }        }    } 

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>


Java Thread Collaboration Join ()

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.