您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 招标投标 > Java第09章--1(多线程)
JavaJavapenggang@mail.hust.edu.cn1.2.3.4.5.6.(Daemon)7.(ThreadGroup)111WebWord/Excel1Unix/LinuxWindowsCC++JavaThreadJavaCPUjava.lang.ThreadThreadThreadThread2JavaJavajava.langThreadThread2ThreadCPUrun()run()Javarun()run()run()run()MPEG2ThreadpublicThread()publicThread(Runnabletarget)publicThread(Runnabletarget,Stringname)publicThread(ThreadGroupgroup,Runnabletarget,Stringname)grouptargetrun()nametargetRunnableRunnablevoidrun()RunnableThreadRunnablenull2Threadvoidrun()voidstart()throwsIllegalThreadStateExceptionvoidsleep(longmillis)CPUvoidinterrupt()staticbooleaninterrupted()booleanisInterrupted()2ThreadbooleanisAlive()startrunstaticThreadcurrentThread()voidsetName(StringthreadName)StringgetName()voidjoin([longmillis[,intnanos]])mills+nanos2Threadvoiddestroy()voidstop()deprecatedvoidsuspend()/voidresume()/deprecatedstaticvoidyield()finalvoidsetPriority(intp)finalvoidnotify()/notifyAll()/wait(longtimeout)Object/2Threadrun()Threadrun()targetnullThreadThread7.5,P485:ThreadTest.java2RunnableThreadrun()RunnableRunnable7.6,P486:ThreadTester.java2RunnableoverriderunpublicclassThreadTesterimplementsRunnableRunnableRunnablerun1=newThreadTester(Therad1);ThreadRunnablestartThread(run1).start();3newThread()NewThreadRunnablestart()Blockstop()stop()Deadyield()stop()run()exit..suspend()sleep()wait()I/Oresume()notify()/notifyAll()sleeptimeoutI/O.waitingsleepingsuspendingreadyrunning3isAlive()isAlive()truefalsejoin()yield()readymyThreadthd=newMyThread();thd.start();…thd.join();//thd…//mainjoin//JDK1.0.2join(inttime)time3sleep()sleep()InterruptedExceptiontry{sleep(100);}catch(InterruptedExceptione){//sleeptimeout…//}suspend()resume()suspend()suspend()suspend()resume()MethodTest.javaAppletClock.java4Java4JavaThread.MIN_PRIORITY1(2~4)Thread.NORM_PRIORITY5Thread.MAX_PRIORITY10(6~9)Thread.MAX_PRIORITYThread.NORM_PRIORITYintgetPriority()voidsetPriority(intp)TwoThreadsTest.java5——PipethreadmyWritermyReadermyWritermyReaderPipethread.java5——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=2r5——Java“”objectlockflag“”Java“”volatilesynchronizedsynchronizedvolatilesynchronized“”synchronized“”pushpop121.“”2.synchronized3.synchronizedstallwaitnotify4.synchronized5————putget:5——Javawait()notify()/notifyAll()java.lang.Objectwait()“”waitnotify()/notifyAll()notify()/notifyAll()wait()synchronizedProducerConsumerTest.java7.8P503SynchronousTest.java22RunnableclassProducerimplementRunnable{WareHousew;…}classConsumerimplementRunnable{WareHousew;…}WareHousec=newWareHouse();Runnablep1=Producernew(c);Runnablec1=Consumernew(c);Threadtp=newThread(p1);Threadtc=newThread(c1);tp.start();tc.start();5——Synchronized(b){synchronized(a){…}}Synchronized(a){synchronized(b){…}}Synchronized(a){synchronized(b){…}}Synchronized(a){synchronized(b){…}}6(Daemon)/requestdaemonrun()publicbooleanisDaemon()publicvoidsetDaemon(boolean),7(ThreadGroup)suspend,resumeThreadGroup(StringgroupName)ThreadGroup(ThreadGrouptg,StringgroupName)Thread(ThreadGrouptg,Runnablero)Thread(ThreadGrouptg,Runnablero,StringthreadName)7(ThreadGroup)intactiveCount()intenumerate(Thread[]list[,booleanrecursive])//false:intenumerate(ThreadGroup[]list[,booleanrecursive])getMaxProirity/setmaxProirity/getNamegetParent/parentOf/1.RuannableThread2.Javarun()Thread3.4.Appletstart()stop()5.6.synchronized7.waitnotify/FileI/O
本文标题:Java第09章--1(多线程)
链接地址:https://www.777doc.com/doc-6147837 .html