|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
前些天,在CSDN上看到了一个消息,说是ASP.NETAJAX成功在Linux上运行,这一点对我触动很大,而且引发了我许多感叹,所以想写出来分享一下。MissedNotificationAmissednotificationoccurswhenthreadBtriestonotifythreadA,butthreadAisnotyetwaitingforthenotification.InamultithreadedenvironmentlikeJava,youdon’thavemuchcontroloverwhichthreadrunsandforhowlong.Thisuncertaintycanleadtoasituationinwhichmostofthetimeanapplicationisrun,threadAiswaitingbeforethreadBdoesthenotification.Butoccasionally,threadBdoesthenotificationbeforethreadAiswaiting.Thismissednotificationscenariocanbequitedangerous.MissedNotification指:线程B试图关照线程A,但线程A并没有在守候关照。这并非不成能呈现的。在多线程情况中,我们不克不及把持哪一个线程实行,实行多长工夫,这类不断定有大概招致在一个线程守候之前就先行关照,这是一种很伤害的情形。以下程序会呈现这类情形:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassMissedNotify{privateObjectproceedLock;publicMissedNotify(){proceedLock=newObject();}publicvoidwaitProceed()throwsInterruptedException{print("inwaitProceed()-begin");synchronized(proceedLock){print("beginsynchronizedwait...");proceedLock.wait();print("endsynchronizedwait...");}print("inwaitProceed()-end");}publicvoidnotifyProceed(){print("innotifyProceed()-begin");synchronized(proceedLock){print("beginsynchronizednotify...");proceedLock.notifyAll();print("endsynchronizednotify...");}print("innotifyProceed()-end");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalMissedNotifymn=newMissedNotify();RunnablerunA=newRunnable(){publicvoidrun(){try{Thread.sleep(1000);//wait()后实行mn.waitProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadA=newThread(runA,"threadA");threadA.start();RunnablerunB=newRunnable(){publicvoidrun(){try{Thread.sleep(500);//notify()先实行mn.notifyProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadB=newThread(runB,"threadB");threadB.start();try{Thread.sleep(10000);}catch(InterruptedExceptione){}print("interruptthreadA...");threadA.interrupt();}}输入了局:threadB-innotifyProceed()-beginthreadB-beginsynchronizednotify...threadB-endsynchronizednotify...threadB-innotifyProceed()-endthreadA-inwaitProceed()-beginthreadA-beginsynchronizedwait...main-interruptthreadA...java.lang.InterruptedExceptionatjava.lang.Object.wait(NativeMethod)atjava.lang.Object.wait(Object.java:429)atorg.tju.msnrl.jonathan.thread.chapter8.MissedNotify.waitProceed(MissedNotify.java:35)atorg.tju.msnrl.jonathan.thread.chapter8.MissedNotify$1.run(MissedNotify.java:66)atjava.lang.Thread.run(Thread.java:534)办理办法:只需加一个标记位:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassMissedNotifyFix{privateObjectproceedLock;privatebooleanokToProceed;publicMissedNotifyFix(){okToProceed=false;proceedLock=newObject();}publicvoidwaitProceed()throwsInterruptedException{print("inwaitProceed()-begin");synchronized(proceedLock){while(!okToProceed){print("beginsynchronizedwait...");proceedLock.wait();print("endsynchronizedwait...");}}print("inwaitProceed()-end");}publicvoidnotifyProceed(){print("innotifyProceed()-begin");synchronized(proceedLock){okToProceed=true;print("beginsynchronizednotify...");proceedLock.notifyAll();print("endsynchronizednotify...");}print("innotifyProceed()-end");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalMissedNotifyFixmn=newMissedNotifyFix();RunnablerunA=newRunnable(){publicvoidrun(){try{Thread.sleep(1000);mn.waitProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadA=newThread(runA,"threadA");threadA.start();RunnablerunB=newRunnable(){publicvoidrun(){try{Thread.sleep(500);mn.notifyProceed();}catch(InterruptedExceptione){e.printStackTrace();}}};ThreadthreadB=newThread(runB,"threadB");threadB.start();try{Thread.sleep(10000);}catch(InterruptedExceptione){}print("interruptthreadA...");threadA.interrupt();}}输入了局:threadB-innotifyProceed()-beginthreadB-beginsynchronizednotify...threadB-endsynchronizednotify...threadB-innotifyProceed()-endthreadA-inwaitProceed()-beginthreadA-inwaitProceed()-endmain-interruptthreadA...EarlyNotificationIfathreadisnotifiedwhilewaiting,buttheconditionthethreadiswaitingforhasnotyetbeenmet,thethreadhasreceivedanearlynotification.Anearlynotificationcanalsooccuriftheconditionisbrieflymetbutquicklychangessoit’snolongermet.Thismightsoundstrange,butearlynotificationcanhappenduetosubtleerrorsinthecode(generallywhenanifisusedinsteadofawhile).Earlynotification是指:当一个守候线程被关照的时分,它守候的前提不再满意,这时候我们说,线程收到了一个earlynotification。Earlynotification在守候的前提刹时满意然后立即改动不再满意时,也会产生。一般在一个必要利用while的中央利用了if。Earlynotification的代码:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;importjava.util.*;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassEarlyNotify{privateListlist;publicEarlyNotify(){list=Collections.synchronizedList(newLinkedList());}publicStringremoveItem()throwsInterruptedException{print("enteringremoveItem()...");synchronized(list){if(list.isEmpty()){print("listwait...begin");list.wait();print("listwait...end");}Stringitem=(String)list.remove(0);print("leaveingremoveItem()...");return"youremove"+item;}}publicvoidaddItem(Stringitem){print("enteringaddItem()...");synchronized(list){list.add(item);print("listnotify...begin");list.notifyAll();print("listnotify...end");}print("leaveaddItem()...");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalEarlyNotifyen=newEarlyNotify();RunnablerunA=newRunnable(){publicvoidrun(){try{Stringitem=en.removeItem();print("inrun()removeitem"+item);}catch(InterruptedExceptione1){print("interrupted");}catch(Exceptione2){print("exception"+e2.getMessage());}}};RunnablerunB=newRunnable(){publicvoidrun(){en.addItem("hello");}};try{ThreadthreadA=newThread(runA,"threadA");threadA.start();Thread.sleep(500);ThreadthreadB=newThread(runA,"threadB");threadB.start();Thread.sleep(500);ThreadthreadC=newThread(runB,"threadC");threadC.start();Thread.sleep(10000);threadA.interrupt();threadB.interrupt();}catch(InterruptedExceptione1){}catch(Exceptione2){}}}输入了局:threadA-enteringremoveItem()...threadA-listwait...beginthreadB-enteringremoveItem()...threadB-listwait...beginthreadC-enteringaddItem()...threadC-listnotify...beginthreadC-listnotify...endthreadA-listwait...endthreadA-leaveingremoveItem()...threadA-inrun()removeitemyouremovehellothreadB-listwait...endthreadC-leaveaddItem()...threadB-exceptionIndex:0,Size:0准确的代码:/**Createdon2005-7-14**JavaThreadProgramming-PaulHyde*Copyright?1999SamsPublishing*JonathanQ.Bo进修条记**/packageorg.tju.msnrl.jonathan.thread.chapter8;importjava.util.*;/***@authorJonathanQ.BofromTJUMSNRL**Email:jonathan.q.bo@gmail.com*Blog:blog.csdn.net/jonathan_q_bo*blog.yesky.net/jonathanundersun**EnjoyLifewithSun!**/publicclassEarlyNotifyFix{privateListlist;publicEarlyNotifyFix(){list=Collections.synchronizedList(newLinkedList());}publicStringremoveItem()throwsInterruptedException{print("enteringremoveItem()...");synchronized(list){while(list.isEmpty()){//usewhileinsteadofifprint("listwait...begin");list.wait();print("listwait...end");}Stringitem=(String)list.remove(0);print("leaveingremoveItem()...");return"youremove"+item;}}publicvoidaddItem(Stringitem){print("enteringaddItem()...");synchronized(list){list.add(item);print("listnotify...begin");list.notifyAll();print("listnotify...end");}print("leaveaddItem()...");}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalEarlyNotifyFixen=newEarlyNotifyFix();RunnablerunA=newRunnable(){publicvoidrun(){try{Stringitem=en.removeItem();print("inrun()removeitem"+item);}catch(InterruptedExceptione1){print("interrupted");}catch(Exceptione2){print("exception"+e2.getMessage());}}};RunnablerunB=newRunnable(){publicvoidrun(){en.addItem("hello");}};try{ThreadthreadA=newThread(runA,"threadA");threadA.start();Thread.sleep(500);ThreadthreadB=newThread(runA,"threadB");threadB.start();Thread.sleep(500);ThreadthreadC=newThread(runB,"threadC");threadC.start();Thread.sleep(10000);threadA.interrupt();threadB.interrupt();}catch(InterruptedExceptione1){}catch(Exceptione2){}}}输入了局:threadA-enteringremoveItem()...threadA-listwait...beginthreadB-enteringremoveItem()...threadB-listwait...beginthreadC-enteringaddItem()...threadC-listnotify...beginthreadC-listnotify...endthreadA-listwait...endthreadA-leaveingremoveItem()...threadA-inrun()removeitemyouremovehellothreadB-listwait...endthreadB-listwait...beginthreadC-leaveaddItem()...threadB-interrupted
令人可喜的是java现在已经开源了,所以我想我上述的想法也许有一天会实现,因为java一直都是不断创新的语言,每次创新都会给我们惊喜,这也是我喜欢java的一个原因。 |
|