|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
你精通任何一门语言就最强大。现在来看,java的市场比C#大,C#容易入手,比较简单,java比较难CubbyHoleExampleTheclassCubbyHole(seeListing8.9)simulatesacubbyhole.Acubbyholeisaslotthatcanhaveonlyoneiteminitatatime.Onethreadputsanitemintotheslotandanotherthreadtakesitout.Ifathreadtriestoputanitemintoacubbyholethatisalreadyoccupied,thethreadblocksuntiltheslotisavailable.Ifathreadtriestoremoveanitemfromanemptycubbyhole,thethreadblocksuntilanitemisadded.Inthisexample,theslotisareferencetoanobject.Thistechniqueallowsobjectstobehandedofffromonethreadtoanotherinathread-safemanner.CubbyHole是指一个通道,某一时候只能包容一个工具,一个线程放,另外一个线程取。假如已有工具,不克不及再放,直到工具被掏出。假如没有工具,则不克不及取,直到放出来新工具。上面给出一个线程平安的办理计划:CubbyHole.java/**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!**/publicclassCubbyHole{privateObjectobj;publicCubbyHole(){obj=null;}publicsynchronizedvoidputIn(Objectobj)throwsInterruptedException{print("putIn()...begin");while(this.obj!=null){//守候,直到数据被掏出print("putIn()wait...begin");wait();print("putIn()wait...end");}this.obj=obj;//增加新对象notifyAll();//关照别的线程:有对象可取print("putIn()...end");}publicsynchronizedObjecttakeOut()throwsInterruptedException{print("takeOut()...begin");while(obj==null){//守候,直到无数据能够取print("takeOut()wait...begin");wait();print("takeOut()wait...end");}Objecttemp=obj;obj=null;//将原数据清空notifyAll();//关照别的线程:数据已掏出,可填新数据print("takeOut()...end");returntemp;}publicvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}}CubbyHoleMain.java/**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!**/publicclassCubbyHoleMain{publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){finalCubbyHolech=newCubbyHole();RunnablerunA=newRunnable(){publicvoidrun(){try{Thread.sleep(500);//存心晚一些实行,让别的线程先取数据Stringstr;str="multithreads";ch.putIn(str);//可一向存print("run()putIn()"+str);str="programming";ch.putIn(str);print("run()putIn()"+str);str="withjava";ch.putIn(str);print("run()putIn()"+str);}catch(InterruptedExceptione){print("exception"+e.getMessage());}}};RunnablerunB=newRunnable(){publicvoidrun(){try{Objectobj;obj=ch.takeOut();print("run()takeOut()"+obj);Thread.sleep(500);//先取,后存obj=ch.takeOut();//可一向取print("run()takeOut()"+obj);obj=ch.takeOut();print("run()takeOut()"+obj);}catch(InterruptedExceptione){print("exception"+e.getMessage());}}};ThreadthreadA=newThread(runA,"threadA");threadA.start();ThreadthreadB=newThread(runB,"threadB");threadB.start();}}输入了局:threadB-takeOut()...beginthreadB-takeOut()wait...beginthreadA-putIn()...beginthreadA-putIn()...endthreadB-takeOut()wait...endthreadB-takeOut()...endthreadB-run()takeOut()multithreadsthreadA-run()putIn()multithreadsthreadA-putIn()...beginthreadA-putIn()...endthreadA-run()putIn()programmingthreadA-putIn()...beginthreadA-putIn()wait...beginthreadB-takeOut()...beginthreadB-takeOut()...endthreadB-run()takeOut()programmingthreadB-takeOut()...beginthreadB-takeOut()wait...beginthreadA-putIn()wait...endthreadA-putIn()...endthreadA-run()putIn()withjavathreadB-takeOut()wait...endthreadB-takeOut()...endthreadB-run()takeOut()withjavaUsingjoin()toWaitforaThreadtoDieThejoin()methodofThreadcanbeusedtocausethecurrentthreadtoblockwaitingforthespecifiedthreadtodie.Thisisarelativelycrudeformofinter-threadcommunication,butonoccasionitcanbeuseful.IfthreadXrunsthecodejoin()办法使以后线程堵塞,直到指定的线程出生。try{threadY.join()}catch(InterruptedExceptionx){}threadXwillblockwaitingforthreadYtodie.IfthreadXisinterruptedwhileinsidejoin(),itwillthrowanInterruptedException.Therearethreeversionsofthejoin()methodavailableinThread,allofwhicharepublic:join(),join(long),andjoin(long,int).Additionally,noneofthesemethodscanbeoverriddeninasubclassbecausetheyarealldeclaredfinal.在Thread类中,供应了三个函数:join(),join(long),join(long,int),都被声明为final,不成被子类overridejoin()publicfinalvoidjoin()throwsInterruptedExceptionThejoin()methodcausesthecurrentthreadtoblockandwaitanunlimitedamountoftimeforthisthreadtodie.ThecurrentthreadwillthrowanInterruptedExceptionifinterruptedwhilewaitingforthespecifiedthreadtodie.join()堵塞以后线程直至其出生,假如守候时被interrupted,会抛出InterruptedExceptionjoin(long)publicfinalsynchronizedvoidjoin(longmsTimeout)throwsInterruptedException,IllegalArgumentException//RuntimeExceptionThejoin(long)methodcausesthecurrentthreadtoblockandwaituptomsTimeoutmillisecondsforthespecifiedthreadtodie.IfmsTimeoutis0,thecurrentthreadwillnevertimeoutandwillwaitforeverforthespecifiedthreadtodie(justlikejoin()).IfmsTimeoutislessthan0,anIllegalArgumentExceptionisthrown.ThecurrentthreadwillthrowanInterruptedExceptionifinterruptedwhilewaitingforthespecifiedthreadtodie.join(long)使以后线程堵塞必定工夫(毫秒)守候其出生,假如设准时间为0,则无穷守候不会超时,假如参数小于0,会抛出IllegalArgumentException非常join(long,int)准确到纳秒,一样平常假造机未完成此办法JoinDemo/**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!**/publicclassJoinDemo{/***天生一个线程*@paramname线程名*@paramnapTime线程存活工夫*@return前往重生成的线程*/publicstaticThreadlaunch(Stringname,longnapTime){finallongsleepTime=napTime;Runnablerun=newRunnable(){publicvoidrun(){try{print("run()...begin");Thread.sleep(sleepTime);}catch(InterruptedExceptione){print("interrupted");}finally{print("run()...end");}}};Threadthread=newThread(run,name);thread.start();//入手下手实行returnthread;}publicstaticvoidprint(Stringmsg){Stringtemp=Thread.currentThread().getName();System.out.println(temp+"-"+msg);}publicstaticvoidmain(String[]args){Thread[]t=newThread[3];t[0]=launch("threadA",2000);t[1]=launch("threadB",1000);t[2]=launch("threadC",3000);for(inti=0;i<t.length;i++){try{Stringindex="t["+i+"]";Stringname=t[i].getName();print(name+"isalive?"+t[i].isAlive());print(name+"beginjoin");longstartTime=System.currentTimeMillis();t[i].join();longendTime=System.currentTimeMillis();print(name+"endjoin,costs"+(endTime-startTime)+"ms");}catch(InterruptedExceptione){e.printStackTrace();}}}}输入了局:main-threadAisalive?truemain-threadAbeginjointhreadA-run()...beginthreadB-run()...beginthreadC-run()...beginthreadB-run()...endthreadA-run()...endmain-threadAendjoin,costs2000msmain-threadBisalive?falsemain-threadBbeginjoinmain-threadBendjoin,costs0msmain-threadCisalive?truemain-threadCbeginjointhreadC-run()...endmain-threadCendjoin,costs1000ms
什么时候上述的三种开发工具能和三为一,什么时候java的竞争力才更强,才有机会拉拢更多的程序员投入到对java的开发上,因为到时的开发工具将会比.net的更简单。还有一点也很关键,什么时候java推出的jsf能成为真正意义上的标准。 |
|