您好,欢迎访问三七文档
JavaUnix/LinuxWindowsCC++JavaThreadJavaCPUjava.lang.ThreadThreadThreadThreadJavaJavaJava.langThreadThreadThreadCPUrun()run()Javarun()run()run()run()JPEGThreadpublicThread(ThreadGroupgroup,Runnabletarget,Stringname)grouptargetrun()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()RunnableCPURunnableThreadRunnablerun()Thread.currentThread()RunnablenewThread()NewThreadRunnablestart()NotRunnablestop()stop()Deadyield()run()stop()..suspend()sleep()wait()I/Oresume()notify()/notifyAll()I/O.newThreadmyThreadthd=newmyThread();IllegalThreadStateExceptionRunnablestart()thd.start();run()(Runnable)Running)JavaNotRunnable¾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()truefalsejoin()yield()myThreadthd=newMyThread();thd.start();…thd.join();//thd…join(inttime)timesleep()sleep()InterruptedExceptiontry{sleep(100)}catch(InterruptedExceptione){…//}suspend()resume()suspend()suspend()suspend()resume()JavaJavaThread.MIN_PRIORITY1(2~4)Thread.NORM_PRIORITY5Thread.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“”volatilesynchronizedclassstack{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
本文标题:Java多线程
链接地址:https://www.777doc.com/doc-5104797 .html