java基礎(chǔ)課程14-第十四章多線程_第1頁
java基礎(chǔ)課程14-第十四章多線程_第2頁
java基礎(chǔ)課程14-第十四章多線程_第3頁
java基礎(chǔ)課程14-第十四章多線程_第4頁
java基礎(chǔ)課程14-第十四章多線程_第5頁
已閱讀5頁,還剩62頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

Java基礎(chǔ)課程第十四章簡(jiǎn)本課件由網(wǎng)新()軟件 (以下簡(jiǎn)稱:網(wǎng)新)編制,網(wǎng)新仿真實(shí)訓(xùn)的學(xué)網(wǎng)新享有本課件中的文字?jǐn)⑹?、文檔格式、插圖、等所有信息資料的,受知識(shí)法及法等法律、的保護(hù)。任何個(gè)人或組織網(wǎng)新的 ,網(wǎng)新保留 上節(jié)課回 本課目線程的通訊(難點(diǎn)什么是線一個(gè)程序由一個(gè)或多個(gè)進(jìn)程組成的,一個(gè)進(jìn)程包含一個(gè)或多個(gè)線程進(jìn)程(process)本質(zhì)上是一個(gè)可執(zhí)行的程序。操作系統(tǒng)引入進(jìn)程概念象實(shí)例對(duì)應(yīng)一個(gè)線程多線程則指的是在單個(gè)程序中可以同時(shí)運(yùn)行多個(gè)不同的線什么是線 。 線程的調(diào)搶占式:高優(yōu)先級(jí)的線程搶占classMythreadextendsThreadpublicstaticvoidmain(Stringargs[])Threadt=tryfor(inti=0;i<3;i++)

輸出每個(gè) 暫停1500catch(InterruptedExceptione)}}ThreadThreadrunclassmythreadextendsThread{publicvoidrun**}Runnablerun。classmythreadimplementsRunnable{publicvoidrun(){/*實(shí)現(xiàn)該方法*/}}startMythreadt=newstartrun()Thread(Stringname)構(gòu)造名稱為name classMyThread1extendsThreadpublicstaticvoidmain(Stringargs[])Threadt=

或者使implementsMyThread1ex=new}publicvoidrun()}}Born)Ready)start(Running睡眠(Slee ):線程的執(zhí)行可通過使用sleep()方法來Waiting)waitResume)以說它已被恢復(fù)。阻塞(Blocked)– ,線程就處 狀態(tài)?,F(xiàn)在也不使用stop()方法新線程(新建

線程生命周ThreadstateNe

sleep()timeouorthreadjoin()orinterupt(

OtherwiseBlockedorstart( sleep(orjoin(Runnabl

Schedule

Runnin

run()complete

DeaLocavailabl synchronized(Blockedinobject`swait()pool

notify(interupt()

Blockedinobject`slockpool新死死注意stop()方法已經(jīng)被作

CPUsleep(waityieldCPUclassThreadStateDemoextendsThreadThreadpublicThreadStateDemo()t=newSystem.out.printlntSystem.out.printlnt.start();publicvoidrun()try{ System.out.printlntcatch(InterruptedExceptionIE)System.out.println");}publicstaticvoidmain(Stringargs[])newThreadStateDemo();線程類常用方currentThread()返回當(dāng)前運(yùn)行的Thread對(duì)象。start(run()線程體,由start()方法調(diào)用,當(dāng)run()方法返sleep(intn)使線程睡眠n毫秒,n毫秒后,線程可線程類常用方resume()恢復(fù)掛起的線程,使其處于可運(yùn)行狀態(tài)yield()將CPU控制權(quán)主動(dòng)移交到下一個(gè)可運(yùn)行線setName()設(shè)置線程的名字。isAlive()如果線程已被啟動(dòng)并且未被終止,那么isAlive()返回true。如果返回false,則該線程是線程調(diào)讓處于運(yùn)行狀態(tài)的線程調(diào)用Thread.sleep讓處于運(yùn)行狀態(tài)的線程調(diào)用Thread.yield讓處于運(yùn)行狀態(tài)的線程調(diào)用另一個(gè)線程的join()線程睡眠:Thread.sleep()方publicclassMyThreadextendsThread{publicvoidrun(){for(inttry{Thread.sleep(100);}catch(InterruptedExceptione){}}}publicstaticvoidmain(Stringargs[]){MyThreadt1=newMyThread();MyThreadt2=newMyThread();}}線程讓步:Thead.yield()方publicclassMyThreadextendsThread{publicvoidrun(){for(intSystem.out.println(Thread.currentThread().getName()+"}}publicstaticvoidmain(Stringargs[]){MyThreadt1=newMyThread();MyThreadt2=newMyThread();}}等待其他線程結(jié)束publicclassMachineextendsThread{publicvoidrun(){for(inta=0;a<10;a++)}publicstaticvoidmain(Stringargs[])throwsException{Machinemachine=newMachine();();}}線程類 publicfinalvoidsetDaemon(booleanpublicfinalbooleanisDaemon線程(精靈線程可以用方法publicbooleanisDaemon()確定一個(gè)線程是否守護(hù)線程,也可以用方法publicvoidsetDaemon(boolean)來設(shè)定一個(gè)線程為守護(hù)線程。 JavaThreadNORM_PRIORITYMAX_PRIORITYMIN_PRIORITYfinalvoidsetPriority(intnewp)finalintgetPriority共享資源的競(jìng)當(dāng)多個(gè)線程共些數(shù)據(jù),它們的操作會(huì)競(jìng)爭(zhēng)共享資源,這以一個(gè)生產(chǎn)者(Producer),消費(fèi)者(Consumer)為例,生產(chǎn)

堆棧privateStringintpoint=-1;…publicStringpop()Stringgoods=buffer[point];

push()

}publicvoidpush(Stringgoods){}生產(chǎn)者線classProducerextendsThread{privateStacktheStack;privateStringbuffer[3]buffer[2]buffer1]publicbuffer[3]buffer[2]buffer1]theStack=;

}publicvoidrun(){Stringgoods;for(inti=0;i<200;i++)System.out.println(name+":push"+goods+"to}}消費(fèi)者線classConsumerextendsThreadprivateStringname;publicConsumer(Stacks,Stringname){=name;buffer[3]buffer[2]buffer[3]buffer[2]buffer1];

消費(fèi)者線

}publicvoidrun(){Stringgoods;for(inti=0;i<200;i++){goods=theStack.pop();+":pop"+goods+"from"+theStack.getName());}}}創(chuàng)建生產(chǎn)者和消費(fèi)者線publicclassTestpublicstaticvoidmain(Stringargs[]){Stackstack=newStack("stack1");Producerproducer1=newProducer(stack,"producer1");Consumerconsumer1=newConsumer(stack,"consumer1");}}生產(chǎn) 消費(fèi)堆堆共享資源的競(jìng)打打印結(jié)果producer1:pushgoods0tostack1consumer1:popnullfromstack1producer1:pushgoods0tostack1…線程的同同步是一種保證共享資源完整性publicStringpop(){Stringgoods=return

publicsynchronizedStringpop(){Stringgoods=buffer[point];}}} 堆棧的方法同classStack…publicsynchronizedintgetPoint(){returnpoint;}publicsynchronizedStringpop(){…}publicsynchronizedvoidpush(Stringgoods)}線程同修改完代碼以線程同在Product類的runstack線程 機(jī)制,在Java5之前,我們只能使用synchronized來鎖定。們使用ReentrantLock保證代碼線程安全privatestaticReentrantLocklock=new//執(zhí)行后釋放 堆棧的方法同 堆棧的方法同線程同步的特線程的同步的特publicstaticvoidmain(Stringargs[])Stackstack1=newProducerproducer1=newProducer(stack1,"producer1");Producerproducer2=newProducer(stack1,"producer2");Producerproducer3=newProducer(stack1,"producer3");Consumerconsumer1=newConsumer(stack1,"consumer1");Stackstack2=newStack("stack2");Producerproducer4=newProducer(stack2,"producer4");Consumerconsumer2=newConsumer(stack2,"consumer2");}}線程的同步的特

Stack1

wait()、notify()和notifyAll()方法。Objectfinal調(diào)用notify()方法。notify(waitnotifyAll(wait調(diào)用的線程放棄線程通 S生產(chǎn)者與消費(fèi)者線程通classStack…publicsynchronizedStringpop(){}Stringgoods=buffer[point];point--return}publicsynchronizedvoidpush(Stringgoods){}生產(chǎn)者與消費(fèi)者線程通classStack…publicsynchronizedvoidpush(Stringgoods){}}}生產(chǎn)者與消費(fèi)者線程通publicclassTestpublicstaticvoidmain(Stringargs[]){Stackstack1=newStack("stack1");Producerproducer1=newProducer(stack1,"producer1");Consumerconsumer1=newConsumerconsumer2=new}}}生產(chǎn)者與消費(fèi)者線程通–(1)執(zhí)行this.notifyAll()方法,此時(shí)this的stack1(2)由于point為-1,因此執(zhí)行this.wait()方法,生產(chǎn)者與消費(fèi)者線程通 終止線 實(shí)際編程中,一般是定義一個(gè)標(biāo)志變量,然后通過程序來改變標(biāo)志變量的值,從而控制線run()方法中自然退出。終止線publicclassMyThreadStopextendsThread{inta;booleanflag=false;publicvoidrun(){}}publicvoidsetFlag(boolean_flag){}publicstaticvoidmain(Stringargs[]){try{Thread.sleep(1000);}catch(Exception}}死死classOperatorimplementsRunn

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論