Java Multithreading wait notify join

Source: Internet
Author: User

Wait Notify

A few points to note:

The wait and Notify/notifyall methods must be used in a synchronous code block, which is to lock the calling object first.

When the thread executes wait (), it releases the current lock and then yields the CPU and enters the wait state.

When the Notify/notifyall method is executed, a thread that waits for the lock on the object is awakened, and then continues to execute until the lock is released after execution of the lock-locked area of the Exit object (the synchronized-decorated code block).

As can be seen here, Notify/notifyall () does not release the lock immediately after execution, but waits until the code in the critical section is executed and then released. Therefore, in actual programming, we should try to exit the critical section immediately after calling Notify/notifyall () on the thread. That is, do not write some time-consuming code behind Notify/notifyall ()

Example code:

public class Service {public void TestMethod (Object lock) {try {synchronized (lock) {                SYSTEM.OUT.PRINTLN ("Begin Wait () threadname=" + Thread.CurrentThread (). GetName ());                Lock.wait ();            System.out.println ("End Wait () threadname=" + Thread.CurrentThread (). GetName ());        }} catch (Interruptedexception e) {e.printstacktrace (); }} public void Synnotifymethod (Object lock) {try {synchronized (lock) {SYSTEM.O                        Ut.println ("Begin Notify () Threadname=" + Thread.CurrentThread (). GetName () + "Time="                + System.currenttimemillis ());                Lock.notify ();                Thread.Sleep (5000);                        System.out.println ("End notify () Threadname=" + Thread.CurrentThread (). GetName () + "Time=" + System.currenttimemIllis ());        }} catch (Interruptedexception e) {e.printstacktrace (); }    }}

  

Call Wait () in line 3rd of TestMethod () and call Notify () in Synnotifymethod () on line 17th

As you can see from the code above, wait () and Notify/notifyall () are placed in the synchronous code block to be able to execute. If you do not obtain an appropriate object lock before executing wait () and Notify/notifyall (), you will throw: Java.lang.IllegalMonitorStateException exception.

In line 8th, when the Threada thread executes lock.wait (), this statement releases the acquired object lock lock and discards the CPU to enter the waiting queue.

When another thread executes line 23rd lock.notify (), it wakes Threada, but at this point it does not immediately release the lock , then it sleeps for 5 Seconds (sleep () does not release the lock, in fact sleep () can not be called in the synchronous code block, The lock is not released until the 28th line exits the critical section of the synchronized modifier. At this point, the Threada has the opportunity to obtain a lock released by another thread and start execution from where it is waiting (line 24th).

Notify the order of notifications can not be wrong

Assuming that wait () is executed in thread A, execute notify () in thread B. However, if thread B executes notify () and then ends, thread A does not execute wait (), then thread A will not be able to wake up normally (and can also wake ^~^ with the interrupt () method to throw an exception).

Join

Thread. Join joins the specified thread to the current thread, and can merge two alternately executed threads into sequentially executed threads. For example, thread A's join () method is called in Threads B, and thread B is not resumed until thread a finishes execution.

If a thread a executes the thread.join () statement, the meaning is that the current thread a waits for the thread thread to terminate and return from Thread.Join ()

Thread.Join can also specify a time-out

Java Multithreading wait notify join

Related Article

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.