Java多线程

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Java„„„„„„„„„„„„Unix/LinuxWindowsCC++JavaThreadJavaƒCPUjava.lang.ThreadƒThreadThreadƒThreadJavaJavaJava.langThreadThreadThreadCPUrun()run()Javarun()run()run()run()JPEGThreadpublicThread(ThreadGroupgroup,Runnabletarget,Stringname)ƒgroupƒtargetrun()ƒnametargetRunnableRunnablevoidrun()RunnableThreadRunnablenullrun()Threadrun()targetnullJavaclassSimpleThreadextendsThread{publicSimpleThread(Stringstr){super(str);}publicvoidrun(){for(inti=0;i10;i++){System.out.println(i++getName());try{sleep((int)(Math.random()*1000));}catch(InterruptedExceptione){}}System.out.println(DONE!+getName());}}RunnableThreadrun()RunnablepublicclassClockextendsjava.applet.AppletimplementsRunnable{ThreadclockThread;publicvoidstart(){if(clockThread==null){clockThread=newThread(this,Clock);clockThread.start();}}publicvoidrun(){while(clockThread!=null){repaint();try{clockThread.sleep(1000);}catch(InterruptedExceptione){}}}publicvoidpaint(Graphicsg){Datenow=newDate();g.drawString(now.getHours()+:+now.getMinutes()+:+now.getSeconds(),5,10);}publicvoidstop(){clockThread.stop();clockThread=null;}}ƒrun()javapublicvoidrun()ƒRunnableCPUƒRunnableƒThreadRunnablerun()Thread.currentThread()ƒRunnablenewThread()NewThreadRunnablestart()NotRunnablestop()stop()Deadyield()run()stop()..suspend()sleep()wait()I/Oresume()notify()/notifyAll()I/O.ƒnewThreadmyThreadthd=newmyThread();IllegalThreadStateExceptionƒRunnablestart()thd.start();run()(Runnable)Running)JavaƒNotRunnable¾sleep()¾suspend()¾wait()¾ThreadmyThread=newMyThreadClass();myThread.start();try{myThread.sleep(10000);}catch(InterruptedExceptione){}ƒNotRunnable¾sleep()¾resume()¾notify()notifyAll();¾I/OI/Oresume()ƒDeadrun()stop()//publicvoidrun(){inti=0;while(i100){i++;System.out.println(i=+i);}}//myThreadthd=newMyThread();thd.start();try{thd.sleep(10000);}catch(InterruptedExceptione){}thd.stop();stop()stop()run()//publicvoidrun(){while(true){…//if(time_to_die){Thread.currentThread().stop();}}}publicclassXyzimplementsRunnable{privatebooleantimeToQuit=falsepublicvoidrun(){while(!timeToQuit){…}}publicvoidstopRunning(){timeToQuit=true;}}publicclassControlThread{privateXyzr=newXyz();privateThreadt=newThread(r);publicvoidstartThread(){t.start();}publicvoidstopThread(){t.stopRunning();}}„isAlive()isAlive()truefalse„join()„yield()myThreadthd=newMyThread();thd.start();…thd.join();//thd…join(inttime)time„sleep()sleep()InterruptedExceptiontry{sleep(100)}catch(InterruptedExceptione){…//}„suspend()resume()suspend()suspend()suspend()resume()JavaJava„Thread.MIN_PRIORITY1(2~4)„Thread.NORM_PRIORITY5„Thread.MAX_PRIORITY10(6~9)Thread.MAX_PRIORITYThread.NORM_PRIORITYintgetPriority()voidsetPriority(intp)ƒ1.1PipedOutputStreamPipedInputStreamoutStreaminStream22.21mssm.write(s)s=m.read()write()read()PipethreadmyWritermyReadermyWritermyReaderclassmyWriterextendsThread{privatePipedOutputStreamoutStream;//privateStringmessages[]={Monday,Tuesday,Wednsday,Thursday,Friday,Saturday,Sunday};publicmyWriter(PipedOutputStreamo){outStream=o;}publicvoidrun(){PrintStreamp=newPrintStream(outStream);for(inti=0;imessages.length;i++){p.println(messages[i]);p.flush();System.out.println(Write:+messages[i]);}p.close();p=null;}}classmyReaderextendsThread{privatePipedInputStreaminStream;//publicmyReader(PipedInputStreami){inStream=i;}publicvoidrun(){Stringline;DataInputStreamd;booleanreading=true;d=newDataInputStream(inStream);while(reading&&d!=null){try{line=d.readLine();if(line!=null)System.out.println(”Read:+line);elsereading=false;}catch(IOExceptione){}}try{Thread.currentThread().sleep(4000);}catch(InterruptedExceptione){}}}publicclassPipethread{publicstaticvoidmain(Stringargs[]){PipethreadthisPipe=newPipethread();thisPipe.process();}publicvoidprocess(){PipedInputStreaminStream;PipedOutputStreamoutStream;PrintStreamprintOut;try{outStream=newPipedOutputStream();inStream=newPipedInputStream(outStream);newmyWriter(outStream).start();newmyReader(inStream).start();}catch(IOExceptione){}}}ƒclassstack{intindex=0;char[]data=newchar[6];publicvoidpush(charc){data[index]=c;index++;}publiccharpop(){index--;returndata[index];}}ABstackApushBpop1data=|a|c|||||index=22Apushdata[index]=‘r’data=|a|c|r||||index=23Aindex++ABBpop()‘c’data=|a|c|r||||index=14Aindex++data=|a|c|r||||index=2r1210112classbank{staticdoublebalance;publicboolean(doubleamount){if(balance=amount){balance-=amount;returntrue;}elsereturnfalse;}}Java“”“”Java“”ƒvolatileƒsynchronizedclassstack{intindex=0;char[]data=newchar[6];publicsynchronizedvoidpush(charc){data[index]=c;index++;}publicsynchronizedcharpop(){index--;returndata[index];}}classbank{staticvolatiledoublebalance;public[syn…]boolean(doubleamt){if(balance=amt){balance-=amt;returntrue;}elsereturnfalse;}}classstack{…publicvoidpush(charc){synchronized(this){data[index]=c;index++;}}publiccharpop(){…}}synchronized“”synchronized“”pushpop12synchronizedvolatileƒ——putget:ƒƒJavawait()notify()/notifyAll()wait()“”waitnotify()/notifyAll()“”notify()/notifyAll()wait()synchronizedclassCubbyHole{privateintseq;privatebooleanavailable=false;//publicsynchroni

1 / 48
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功