Use of the Java multithreaded 16:join ()

Source: Internet
Author: User

Before explaining the join () method, make sure you are familiar with the Wait ()/notify ()/notifyall () mechanism. You can refer to the previous notes

The join () method works by waiting for the thread to be destroyed. The join () method responds to a very real problem, such as the execution time of the main thread is 1s, the execution time of the sub-thread is 10s, but the main thread relies on the results of the child thread execution, what should I do? Can be like a producer/consumer model, a buffer, sub-thread execution of the data in the buffer, notify the main thread, the main thread to fetch, so that the main thread will not waste time. Another way is to join ().


Before the join () method





The main thread above wants to wait for a child thread, but the main thread prints first, if you want to implement the main thread wait.



Use Join () to solve




using join successfully let the main thread wait, in fact join inside or use wait, so join is put in lock




Join () method and exception


Above is the THREADB call A.join (), in fact, according to the source of the join is the current B-line lock a thread object, wait, the B thread waits for a thread notify, and to interrupt B thread will be an exception, but a thread or continue, a thread is unaffected




Use of the Join (long) method



The above code shows that the join (long) and sleep (long) implementations have the same effect

A key point of the Join () method is to differentiate between the sleep () methods. Join (2000) is also possible, indicating that the thread that calls the join () method waits at most 2000ms, and the difference is:

Sleep (2000) does not release the lock, and join (2000) releases the lock because the join () method internally uses wait () and therefore releases the lock. Take a look at the source of join (2000) and know that join () is actually the same as join (2000), nothing more than join (0):

   Public Final synchronized voidJoinLongMillis)      throwsinterruptedexception {      LongBase =System.currenttimemillis ();      Longnow = 0;      if(Millis < 0) {              Throw NewIllegalArgumentException ("Timeout value is negative");     }      if(Millis = = 0) {          while(IsAlive ()) {Wait (0);         }}Else {          while(IsAlive ()) {         LongDelay = Millis-Now ;         if(Delay <= 0) {              Break;         } wait (delay);now = System.currenttimemillis ()-Base;         }     }}

Lines 12th and 20th should already be clear.

And sleep doesn't release the lock.




Sleep (long) method does not put locks



The above example shows that join is locked and sleep is not locked.

The following example proves another problem with join, because join IS wait implementation, after being awakened by another thread, it grabs the lock, and it is the first to grab the lock.





The code after the method join () runs prematurely-----unexpected



Explain what happened to the accident





It can be clearly seen that the occurrence of the situation is a variety of reasons is that the join will grab the lock, after execution, then put the lock and then execute the lock after the code, but also because of the randomness of the thread, the situation is various, join not every time will grab the lock






Use of the Java multithreaded 16: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.