Java Thread Programming 1.8.2-Inter-thread Communication

Source: Internet
Author: User
Missed NotificationA missed notification occurs when threadb tries to handle Y threada, but threada is not yet waiting for the notification. in a multithreaded environment like Java, you don't have much control over which thread runs and for how long. this uncertainty can lead to a situation in which most of the time an application is run, threada is waiting before threadb does the notification. but occasionally, threa DB does the notification before threada is waiting. This missed notification scenario can be quite dangerous. Missed notification indicates that thread B tries to notify thread A, but thread A is not waiting for the notification. This is not impossible. In a multi-threaded environment, we cannot control which thread to execute and how long it takes to execute. This uncertainty may lead to a notification before a thread waits. This is a very dangerous situation. This occurs in the following program:/** created on 2005-7-14 ** Java Thread Programming-Paul Hyde * copyright? 1999 SAMs publishing * Jonathan Q. bo Study Notes **/package Org. tju. msnrl. jonathan. thread. chapter8;/*** @ author Jonathan Q. bo from tju msnrl ** E-mail: jonathan.q.bo@gmail.com * blog: blog.csdn.net/jonathan_q_bo * blog.yesky.net/jonathanundersun ** enjoy life with sun! **/Public class missednotify {private object proceedlock; Public missednotify () {proceedlock = new object ();} public void waitproceed () throws interruptedexception {print ("in waitproceed () -Begin "); synchronized (proceedlock) {print (" begin synchronized wait... "); proceedlock. wait (); print ("End synchronized wait... ");} print (" in waitproceed ()-end ");} public void policyproceed () {print (" in Notifyproceed ()-begin "); synchronized (proceedlock) {print (" begin synchronized y... "); proceedlock. policyall (); print ("End synchronized married y... ");} print (" in notifyproceed ()-end ");} public static void print (string MSG) {string temp = thread. currentthread (). getname (); system. out. println (temp + "-" + MSG);} public static void main (string [] ARGs) {final missednotify Mn = new missedn Otify (); runnable Runa = new runnable () {public void run () {try {thread. sleep (1000); // run Mn after wait. waitproceed ();} catch (interruptedexception e) {e. printstacktrace () ;}}; thread threada = new thread (Runa, "threada"); threada. start (); runnable runb = new runnable () {public void run () {try {thread. sleep (500); // sort y () first execute Mn. policyproceed ();} catch (interruptedexception e) {e. printstacktrace ();}} }; Thread threadb = new thread (runb, "threadb"); threadb. start (); try {thread. sleep (10000);} catch (interruptedexception e) {} print ("interrupt thread... "); threada. interrupt () ;}} output result: threadb-In notifyproceed ()-beginthreadb-begin synchronized y... threadb-end synchronized running y... threadb-In notifyproceed ()-endthreada-In waitproceed ()-beginthreada-begin synchronized wait... Main-interrupt thread... java. lang. interruptedexception at java. lang. object. wait (native method) at java. lang. object. wait (object. java: 429) at Org. tju. msnrl. jonathan. thread. chapter8.missednotify. waitproceed (missednotify. java: 35) at Org. tju. msnrl. jonathan. thread. chapter8.missednotify $ 1.run( missednotify. java: 66) at java. lang. thread. run (thread. java: 534) solution: Add a flag:/** created on ** Java Thread Programming-Paul Hyde * copyright? 1999 SAMs publishing * Jonathan Q. bo Study Notes **/package Org. tju. msnrl. jonathan. thread. chapter8;/*** @ author Jonathan Q. bo from tju msnrl ** E-mail: jonathan.q.bo@gmail.com * blog: blog.csdn.net/jonathan_q_bo * blog.yesky.net/jonathanundersun ** enjoy life with sun! **/Public class missedpolicyfix {private object proceedlock; private Boolean oktoproceed; Public missedpolicyfix () {oktoproceed = false; proceedlock = new object ();} public void waitproceed () throws interruptedexception {print ("in waitproceed ()-begin"); synchronized (proceedlock) {While (! Oktoproceed) {print ("begin synchronized wait... "); proceedlock. wait (); print ("End synchronized wait... ") ;}} print (" in waitproceed ()-end ");} public void policyproceed () {print (" in notifyproceed ()-begin "); synchronized (proceedlock) {oktoproceed = true; print ("begin synchronized y... "); proceedlock. policyall (); print ("End synchronized married y... ");} print (" in notifyproceed ()-end ");} public static void print (string MSG) {string temp = thread. currentthread (). getname (); system. out. println (temp + "-" + MSG);} public static void main (string [] ARGs) {final missedpolicyfix Mn = new missedpolicyfix (); runnable Runa = new runnable () {public void run () {try {thread. sleep (1000); Mn. waitproceed ();} catch (interruptedexception e) {e. printstacktrace () ;}}; thread threada = new thread (Runa, "threada"); threada. start (); runnable runb = new runnable () {public void run () {try {thread. sleep (500); Mn. policyproceed ();} catch (interruptedexception e) {e. printstacktrace () ;}}; thread threadb = new thread (runb, "threadb"); threadb. start (); try {thread. sleep (10000);} catch (interruptedexception e) {} print ("interrupt thread... "); threada. interrupt () ;}} output result: threadb-In notifyproceed ()-beginthreadb-begin synchronized y... threadb-end synchronized running y... threadb-In notifyproceed ()-endthreada-In waitproceed ()-beginthreada-In waitproceed ()-endmain-interrupt thread... Early Notification  If a thread is notified while waiting, but the condition the thread is waiting for has not yet been met, the thread has ed Early notification. An early notification can also occur if the condition is briefly met but quickly changes so it's no longer met. this might sound strange, but early notification can happen due to subtle errors in the code (generally when an if is used instead of a while ). early notification means that when a waiting thread is notified, its waiting conditions are no longer met. At this time, we say that the thread receives an early notification. Early notification also happens when the waiting conditions are met immediately and then the changes are no longer met. Generally, if is used in a place where while is needed. Early notification code:/** Created on 2005-7-14 ** Java Thread Programming-Paul Hyde * Copyright? 1999 Sams Publishing * Jonathan Q. bo Study Notes **/package org. tju. msnrl. jonathan. thread. chapter8; import java. util. *;/*** @ author Jonathan Q. bo from tju msnrl ** E-mail: jonathan.q.bo@gmail.com * Blog: blog.csdn.net/jonathan_q_bo * blog.yesky.net/jonathanundersun ** Enjoy Life with Sun! **/Public class EarlyNotify {private List list; public EarlyNotify () {list = Collections. synchronizedList (new inclulist ();} public String removeItem () throws InterruptedException {print ("entering removeItem ()... "); synchronized (list) {if (list. isEmpty () {print ("list wait... begin "); list. wait (); print ("list wait... end ");} String item = (String) list. remove (0); print ("leaveing removeItem ()... "); Return" you remove "+ item;} public void addItem (String item) {print (" entering addItem ()... "); synchronized (list) {list. add (item); print ("list items y... begin "); list. policyall (); print ("list policy... end ");} print (" leave addItem ()... ");} public static void print (String msg) {String temp = Thread. currentThread (). getName (); System. out. println (temp + "-" + msg);} public static v Oid main (String [] args) {final earlypolicy en = new earlypolicy (); Runnable runA = new Runnable () {public void run () {try {String item = en. removeItem (); print ("in run () remove item" + item);} catch (InterruptedException e1) {print ("interrupted");} catch (Exception e2) {print ("exception" + e2.getMessage () ;}}; Runnable runB = new Runnable () {public void run () {en. addItem ("hello") ;}}; try {Th Read threadA = new Thread (runA, "threadA"); threadA. start (); Thread. sleep (500); Thread threadB = new Thread (runA, "threadB"); threadB. start (); Thread. sleep (500); Thread threadC = new Thread (runB, "threadC"); threadC. start (); Thread. sleep (10000); threadA. interrupt (); threadB. interrupt ();} catch (InterruptedException e1) {}catch (Exception e2) {}} output result: threadA-entering removeItem ()... threadA-list w Ait... beginthreadB-entering removeItem ()... threadB-list wait... beginthreadC-entering addItem ()... threadC-list using y... beginthreadC-list notify... endthreadA-list wait... endthreadA-leaveing removeItem ()... threadA-in run () remove item you remove hellothreadB-list wait... endthreadC-leave addItem ()... threadB-exceptionIndex: 0, Size: 0 correct code:/** Created on 2005-7-1 4 ** Java Thread Programming-Paul Hyde * Copyright? 1999 Sams Publishing * Jonathan Q. bo Study Notes **/package org. tju. msnrl. jonathan. thread. chapter8; import java. util. *;/*** @ author Jonathan Q. bo from tju msnrl ** E-mail: jonathan.q.bo@gmail.com * Blog: blog.csdn.net/jonathan_q_bo * blog.yesky.net/jonathanundersun ** Enjoy Life with Sun! **/Public class earlypolicyfix {private List list; public earlypolicyfix (){ List = Collections. synchronizedList (new Collections list ());} Public String removeItem () throws InterruptedException {print ("entering removeItem ()..."); synchronized (list ){ While (list. isEmpty ()){// Use while instead of if print ("list wait... begin "); List. Wait ();Print ("list wait... end ");} String item = (String) list. remove (0); print ("leaveing removeItem ()... "); return" you remove "+ item;} public void addItem (String item) {print (" entering addItem ()... "); synchronized (list) {list. add (item); print ("list items y... begin "); List. policyall ();Print ("list parts y... end ");} print (" Leave additem ()... ");} public static void print (string MSG) {string temp = thread. currentthread (). getname (); system. out. println (temp + "-" + MSG);} public static void main (string [] ARGs) {final earlypolicyfix en = new earlypolicyfix (); runnable Runa = new runnable () {public void run () {try {string item = en. removeitem (); print ("in run () remove item" + item);} catch (interruptedexception E1) {print ("interrupted");} catch (exception E2) {print ("exception" + e2.getmessage () ;}}; runnable runb = new runnable () {public void run () {en. additem ("hello") ;}}; try {thread threada = new thread (Runa, "threada"); threada. start (); thread. sleep (500); thread threadb = new thread (Runa, "threadb"); threadb. start (); thread. sleep (500); thread threadc = new thread (runb, "threadc"); threadc. start (); thread. sleep (10000); threada. interrupt (); threadb. interrupt ();} catch (interruptedexception E1) {}catch (exception E2) {}} output result: threada-entering removeitem ()... threada-list wait... beginthreadb-entering removeitem ()... threadb-list wait... beginthreadc-entering additem ()... threadc-list using y... beginthreadc-list notify... endthreada-list wait... endthreada-leaveing removeitem ()... threada-In run () remove item you remove hellothreadb-list wait... endthreadb-list wait... beginthreadc-leave additem ()... threadb-interrupted  

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.