Java multithreading-join

Source: Internet
Author: User

 

Join can be arranged in sequence. If you want to finish T1 first, then run T2, and recently run T3, you can do this.

T1.start ();

T1.join ();

T2.start ();

T2.join ();

T3.start ()

 

JDK says this:

Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.

Wait for a while until this thread dies

 

Public class threadtest implements runnable {<br/> Public static int A = 0; <br/> Public void run () {<br/> for (int K = 0; k <5; k ++) {<br/> A = a + 1; <br/>}< br/> Public static void main (string [] ARGs) throws exception {<br/> runnable r = new threadtest (); <br/> thread t = new thread (r); <br/> T. start (); <br/> system. out. println (a); <br/>}< br/>} 

 

Is the output result of the program 5? The answer is: possible. At this time, the main thread and the T thread run in parallel.

How can I set the output result to 5! In fact, the join () method provides this function. Join () method, which enables the thread that calls this method to complete execution before this.

 

Public class threadtest implements runnable {<br/> Public static int A = 0; <br/> Public void run () {<br/> for (int K = 0; k <5; k ++) {<br/> A = a + 1; <br/>}< br/> Public static void main (string [] ARGs) throws exception {<br/> runnable r = new threadtest (); <br/> thread t = new thread (r); <br/> T. start (); <br/> T. join (); <br/> system. out. println (a); <br/>}< br/>} 

 

At this time, the input result of the program is always 5.

 

Public final void join () throws interruptedexception {<br/> join (0); <br/>} 

 

That is to say, if it is T. join () = T. join (0) 0 JDK. In this case, a timeout of 0 means to wait forever literally means waiting forever. In fact, it means waiting until t ends.
How is this implemented? Check the JDK code:

 

/** <Br/> * waits at most <code> millis </code> milliseconds for this thread to <br/> * die. a timeout of <code> 0 </code> means to wait forever. <br/> * @ Param millis the time to wait in milliseconds. <br/> * @ exception interruptedexception if any thread has interrupted <br/> * The current thread. the <I> interrupted status </I> of the <br/> * Current thread is cleared when this exception is thrown. <br/> */<br/> Public final synchronized void join (long millis) <br/> throws interruptedexception {<br/> long base = system. currenttimemillis (); <br/> long now = 0; <br/> If (millis <0) {<br/> throw new illegalargumentexception ("timeout value is negative "); <br/>}< br/> If (millis = 0) {<br/> while (isalive () {<br/> wait (0 ); <br/>}< br/>}else {<br/> while (isalive () {<br/> long delay = millis-now; <br/> If (delay <= 0) {<br/> break; <br/>}< br/> wait (Delay); <br/> now = system. currenttimemillis ()-base; <br/>}< br/>} 

 

In fact, the join method is implemented through wait.


Wait () method

After other threads call the wait method of the object, the current thread waits. The thread will release the lock flag it occupies, so that other threads have the opportunity to seize the lock.

 

When the main thread calls T. join, the main thread will lose the lock of the thread object T and wait until the object wakes up the main thread, for example, after exiting.


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.