The art of Java Concurrency Programming reading notes: wait/notification mechanism

Source: Internet
Author: User

Before reading this book, it was probably about wait and notify that, after the thread A that called wait was blocked, the Notify method was called once in addition to a wired thread. Thread A will immediately return from the wait method. After reading the book. It's too superficial to find out what I know.

When a thread calls wait (), it releases the lock that has been acquired.

Enter the waiting state at the same time, not the blocked state. Only wait for other threads to call the Notify () method and release the lock. The current thread is not returned from the Wait () method. It is not enough to give notice. The thread that is also required to issue a notification releases the lock.

However, the invocation of the Notify () method does not imply the release of the lock.

posted the original book demo, slightly made some changes.

/** * */package chapter04;import java.text.simpledateformat;import Java.util.date;import java.util.concurrent.timeunit;/** * 6-11 */public class Waitnotify {private static final SimpleDateFormat SDF = new Simpl  Edateformat ("HH:mm:ss"); static Boolean flag = true;static Object lock = new Object ();p ublic static void Main (string[] args) {Thread waitthread = new Thread (new Wait (), "Waitthread"); Waitthread.star T (); sleepseconds (1); Thread notifythread = new Thread (new Notify (), "Notifythread"); Notifythread.start ();}  Static class Wait implements Runnable {public void run () {synchronized (lock) {///when the condition is not met, continue Wait, lock while (flag) is released at the same time {try {System.out.println (Cuttentthreadname () + "flag is true. Wait @ "+ sdf.format (new Date ())); lock.wait (); System.out.println (Cuttentthreadname () + "gained lock again. Wait @ "+ sdf.format (new Date ()));} catch (Interruptedexception e) {e.printstacktrace ();}} When the condition is met. Finished work System.out.println (Cuttentthreadname () +"Flag is false." Running @ "+ Sdf.format (new Date ()));}}} private static String Cuttentthreadname () {return Thread.CurrentThread (). GetName ();} Static Class Notify implements Runnable {@Overridepublic void Run () {//locking, with Lock's monitorsynchronized (lock) {//Get lock for lock. The notification is then notified that lock locks are not released,//Until lock is released by the current thread, and Waitthread talent returns SYSTEM.OUT.PRINTLN (Cuttentthreadname () + "hold lock" from the Wait method. Notify @ "+ Sdf.format (new Date ())); Lock.notifyall (); flag = False;sleepseconds (5); System.out.println (Cuttentthreadname () + "is releasing lock @" + Sdf.format (new Date ()));} Lock synchronized (lock) {System.out.println (Cuttentthreadname () + "hold lock again again. Sleep @ "+ Sdf.format (new Date ())); Sleepseconds (5); System.out.println (Cuttentthreadname () + "is releasing lock @" + Sdf.format (new Date ()));}}} private static void sleepseconds (int timeout) {try {TimeUnit.SECONDS.sleep (timeout);} catch (Interruptedexception e) { E.printstacktrace ();}}}

the output is:

Waitthread flag is true. Wait @ 20:58:32notifythread hold lock. Notify @ 20:58:33notifythread is releasing lock  @ 20:58:38notifythread hold lock again. Sleep @ 20:58:38notifythread I S releasing lock  @ 20:58:43waitthread gained lock again. Wait @ 20:58:43waitthread flag is false. Running @ 20:58:43

by outputting The results, we can find several problems.

After 1.NotifyThread calls the Notifyall () method, Waitthread does not return immediately from the Wait () method.

Because of this time Notifythread did not release the lock.

2. Program 61 Line,notifythread the first release of the lock, however waitthread do not make up, and did not grab this lock. Still in blocked state.

3. Until 67 lines. Notifythread Release the lock once again. Waitthread Gets the lock, which returns from Wait () to continue running.

4. The thread returns from the Wait () method if the lock required by the synchronized is obtained.

Analysis of the original book after the demo, whether there is the same feeling as me, the previous understanding is so too young too simple.

The art of Java Concurrency Programming reading notes: wait/notification mechanism

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.