欢迎来到天天文库
浏览记录
ID:30777820
大小:307.36 KB
页数:8页
时间:2019-01-03
《java并发编程(11):线程间通信中notify通知的遗漏(含代码)-编程开发技术》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、Java20-0±a3I£-l1£©£。丽關・DAODnoti顼0丫入OAA©£JpAe£©.士沪i處Java20-0±a3I£ll£©£°IB3K4arDAODnotifyrdanAOAA©£-°-uAe£©OIA3dz
2、£°A^fa-gOenotifyfdapAdAA©oUEYdxAi^a£-%^threadA»1A»^aE%waitpAE±od£-threadBdNnotifyAE£->daNii£->threadBf*daEQA»6DEHn6
3、yA£-'p±threadBIE3dsynchronizedzuA
4、e/,e°6£-«threadA6u^aE%wait£-i±a»ad»O±xeEupEzy£_,O±p^±»±3pAIB3rd(
5、[ij£dAA©rdagAuAe♦•八~、八八~/八/♦•八、入、。入XIAA^^oO^IuAeNYE%rOaE^E?°IOAA©pA£^E?IA£°publicclassMissedNotifyextendsObject{privateObjectproceedLock;publieMissedNotify(){print("inMissedNotify()n);proceedLock=
6、newObject();}publievoidwaitToProceed()throwsInterruptedException{print("inwaitToProceed()一entered,f);synchronized(proceedLock){print(,finwaitToProceed()一abouttowait()n);proceedLock.wait();print(HinwaitToProceed()-backfromwait()n);}print(ninwaitToProceed()-leavi
7、ng");}publicvoidproceed(){print(ninproceed()一enteredn);synchronized(proceedLock){print(Hinproceed()一abouttonoti£yAJ_]_()");proceedLock.notifyAll();print(ninproceed()一backfromnotifyAll()n);}print(11inproceed()一leaving11);}privatestaticvoidprint(Stringmsg){String
8、name=Thread.currentThread().getNeme();System.out.printIn(name+n:n+msg);}publicstaticvoidmain(String[]args){finalMissedNotifymn=newMissedNotify();RunnablerunA=newRunnable(){publicvoidrun(){try{//BYAhl000ms£^^OUrunBODpASOOmsE-'//总总。6口专(5瓦weitToProceedfL-1zO^[0lEn
9、otifyAHE-'^owaitE-1//^O^oOi^I^O^pAOAA©Thread.sleep(1000);mn.waitToProceed();}catch(InterruptedExceptionx)x•printStackTrace();}}};ThreadthreadA=newThread(runA,,fthreadA,f);threadA.start();RunnablerunB=newRunnable(){publicvoidrun(){try{//DYAft500ms£^DjOUrunAODpAl
10、OOOmsE-'//EQI^AEIEp-J-OAproceedE-'zO^0lEnotifyAlli-'^owaitE-1//^O^oOi^I^O^pAOAA©Thread.sleep(500);mn.proceed();}catch(InterruptedExceptionx)x.printStackTrace();}}};ThreadthreadB=newThread(runB,lfthreadBlf);threadB.start();try{Thread.sleep(10000);}catch(Interrup
11、tedExceptionx){}//EOf^zd5TwaitxeEuprint(nabouttoinvokeinterrupt()onthreadA11);threadA.interrupt();}}O^DD^a^EglAf0????-OId£oOEOUthreadBOUO^DDmn.proceed£,£©O®Q°O»DYABAE5OOms£-
此文档下载收益归作者所有