資源描述:
《java并發(fā)編程(11):線程間通信中notify通知的遺漏(含代碼)-編程開發(fā)技術(shù)》由會員上傳分享,免費在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、Java20-0±a3I£-l1£?£。麗關(guān)?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£-