




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、1 線程的概1 線程的概 AB多線程的弊端AB多線程的弊端CPU開銷。線程的使用會給系統(tǒng)帶來上下文切換的額外負2 創(chuàng)建線程的2 創(chuàng)建線程的方2.1 Thread,runrun,runrun,run()JVN不會當作線程來運行,會認為是普通的方Thread類的對象并啟動該線程,但是如果沒有重寫run(),什么也不2.2 Thread類的對象并啟動該線程,但是如果沒有重寫run(),什么也不2.2 線程池中,變得可運行,等待獲取cpu 的執(zhí)行權。 阻臨時塞: CPU 使用權,暫時停止運行。直到線程進入2.3 Thread(String name) setName(Stringname) newPr
2、iority) 現(xiàn)取決于底層的操作系統(tǒng)的實現(xiàn)(10 1 5 classThreadDemo1classThreadDemo1extendspublicpublicThreadDemo1(Stringnamesuper(namepublicvoidrun() i = 0;while(i30) ln( this.getName() + + : i = + i); ln(Thread.currentThread().getName()+:i=+ln( Thread.currentThread() = this ); ln(getId()+:id=+super.getId(); ln(getPrior
3、ity()+:Priority=super.getPriority()classpublicicvoidmain(StringThreadDemo1th1newThreadDemo1(線程1); ThreadDemo1th2newThreadDemo1(線程th1.setName(th1th2.setName(th2設置線程優(yōu)先級 1th1.setPriority(10th2.setPriority(7ln(max : + Thread.MAX_PRIORITY ); ln(min : + Thread.MIN_PRIORITY ); ln(nor:+Thread.NORM_PRIORITY)
4、; o存在問題:tickets 存在問題:tickets 2.4 Runnablerun方法Thread類可以理解為一個工人,Runnable的實現(xiàn)類的對象就是這個工人的工作(通過Thread類可以理解為一個工人,Runnable的實現(xiàn)類的對象就是這個工人的工作(通過RunnableThread的構(gòu)造時,Thread.slee(100packagepublicclassDemo1public sicvoidmain(Stringargs) MyRun my = new MyRun();Threadt1=newThread(my); for (i = 0; i 200; i+) ln(main:
5、+i);classMyRunimplementsRunnablepublicvoidrun()for (i = 0; i 0)try catch (erruptedExceptione) ln(Thread.currentThread().getName()+tickets號票); elsepublicclassDemo6public sicvoidmain(Stringargs) MyTicket mt = new MyTicket();Threadt1=newThread(mt); Threadt2=newThread(mt); Threadt3=newThread(mt); Thread
6、t4=newThread(mt); .類名 .類名對象Java中的每個對象都有一個內(nèi)置鎖,只有當對象具有同步方法代碼時,內(nèi)置鎖才會起56sleep() 4 死5 2T1L12T1L1T2 L2T1 L2T2 在等待對publicclasspublicclassDeadLockpublicicvoidmain(Stringargs)newThread(newRunnable(創(chuàng)建線程publicvoidrun()synchronized(刀叉)你不給我筷子我就不給你刀叉try catch (erruptedExceptione) synchronized(筷子.getName(給你刀叉,newT
7、hread(newRunnable(publicvoidrun()synchronized(筷子)你先給我刀叉我再給你筷子try catch (erruptedExceptione) synchronized(刀叉.getName(好吧把筷子給你人5 線程的通whilenotifyAll喚醒,notify容易出現(xiàn)只喚醒本方線程情況,導致程序中的所有線程都在等待。例5 線程的通whilenotifyAll喚醒,notify容易出現(xiàn)只喚醒本方線程情況,導致程序中的所有線程都在等待。例空和 的publicclassDemo10public sicvoidmain(Stringargs) p = ne
8、wro = new ProducConsumer con = new ConsumThread t1 = new Threro生產(chǎn)者); Threadt2newThread(con消費者); /使String name; Stringgender;classProducerimplementsRunnable publicProducer()publicersonp)this.p=都都p成員變manpublicvoidrun()i=while(true)if(i%2=0) = jack; p.gender=man;=p.gender女classConsumeri
9、mplementsRunnable publicConsumer()publicersonp)this.p=publicvoidrun()while(true)man packagepublicpublicDemo10icvoidmain(Stringargs)p=man packagepublicpublicDemo10icvoidmain(Stringargs)p=ro= connewnewThreadt1=newThreadt2=newThread(con, classProducerclassProducerimplementsRunnable publicProducer()publ
10、icersonp)this.p=publicvoidrun()i=while(true)synchronized(p)if(i%2=0) = jack; p.gender=man;=p.gender女classConsumerimplementsRunnable publicConsumer()publicersonp)this.p=jack女 或者manjack女 或者manpublicclassDemo10public sicvoidmain(Stringargs) p = newro = new ProducConsumer con = new Consu
11、mThread t1 = new Threro生產(chǎn)者); Threadt2newThread(con消費者); /使String name; Stringgender;publicsynchronizedvoidset(Stringname,Stringgender)=this.gender=publicsynchronizedvoidread()publicvoidrun()while(true)synchronized(p)ln(name:++-gnder:ln(name:++-gender:classProducerimplementsRu
12、nnable publicProducer()publicersonp)this.p=publicvoidrun()i=while(true)if(i%2=0)p.set(jack,else女classConsumerimplementsRunnable publicConsumer()有獲得執(zhí)行權,并持有了相同的監(jiān)視器(鎖)并調(diào)用 notify 為止。 notify:喚醒持有同一個監(jiān)視器(鎖)wait的第一個線程,例如,餐館有空位置后,等候就餐最久的顧客最先入座。注意:被喚醒的線程是進入了可運行狀態(tài)。等待 cpu?packagepublicclassDemo10public sicvoidm
13、ain(Stringargs) p = newro = new ProducConsumercon=newThreadt1newro生產(chǎn)者publicersonp)this.p=publicvoidrun()while(true) Threadt2Threadt2newThread(con消費者); /使String name; Stringgender;flag=publicsynchronizedvoidset(Stringname,Stringgender)if(flag)try catcherruptedExceptione) = name; this.gender=
14、gender; flag = true; publicsynchronizedvoidread()if(!flag)try catcherruptedExceptione)ln(name:++-gender:flag=false; classclassProducerimplementsRunnable publicProducer()publicersonp)this.p=publicvoidrun()i=while(true)if(i%2=0)p.set(jack,else女classConsumerimplementsRunnable publicConsumer()p
15、ublicersonp)this.p=publicvoidrun()(,notifyAll()(,notifyAll()鎖上 wait2. 線程生命周正常終止 當線程的 run()。注意:Stop方法已過時,就不能再使用這個方法。 classStopThreadimplementsRunnable tag = true; publicvoidrun()i=while(tag) while(true) 上述案例中定義了一個計數(shù)器 i,用來控制 main方法(主線程)的循環(huán)打印次數(shù),在 i上述案例中定義了一個計數(shù)器 i,用來控制 main方法(主線程)的循環(huán)打印次數(shù),在 i到 終止了線程1whil
16、e循環(huán),run方法1也隨之結(jié)束。注意:當計i變?yōu)?線當所有的非UpdateimplementsRunnablei=publicclassDemo8public sicvoidmain(Stringargs StopThread st = new StopThread(); Thread th = new Thread(st, 線程1); fori=0;i100;i+)if(i=50)ln(maini:+i); st.tag = false;classhreadimplementsRunnablepublicvoidrun()while(true)ln(Thread.currentThread().getName(檢trycatcherruptedExceptione)if(i=100)publicclassDemo9public sicvoidmain(Stringargs) = newThreadth=newThread( , update); oThreadThreadjointh.jointh對應的線程th.join語句的線程中,main100之前,main線 onemain100one線程執(zhí)行,onemian線程中,o
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 試論關+于完善山西社會保障制度的思考
- 河南省駐馬店市部分學校2024~2025學年 高二下冊4月質(zhì)量檢測數(shù)學試卷(北師大版)附解析
- 重慶市彭水中學高級高考文綜政治練習短卷外國投資者并購境內(nèi)企業(yè)的規(guī)定
- 棗莊機場建設投資有限公司招聘筆試真題2024
- 社區(qū)大數(shù)據(jù)與社區(qū)信息化政策體系完善基礎知識點歸納
- 歷史建筑群保護社區(qū)婦女權益規(guī)劃基礎知識點歸納
- 2024鋼結(jié)構(gòu)連廊及超危大工程投標方案技術標模板
- 教學設計必修第二章22等差數(shù)列(第一課時)程琬婷
- 制造業(yè)物聯(lián)網(wǎng)平臺安全認證-洞察闡釋
- 區(qū)域性廢棄物處理過程中的能源利用與節(jié)能減排措施
- 安全風險排查管理制度
- 2025-2030年中國基因檢測行業(yè)現(xiàn)狀調(diào)查及發(fā)展前景預測研究報告
- 小學生閱讀指導課件教學
- 《鐵路技術管理規(guī)程》(普速鐵路部分)
- 23秋國家開放大學《液壓氣動技術》形考任務1-3參考答案
- 放線測量復核記錄(帶公式)
- Anderson-局域化的簡介及相關物理圖像(共6頁)
- 超聲科制度匯編【精選文檔】
- 國際政治經(jīng)濟學的主要流派課件
- (完整版)《金屬與石材幕墻工程技術規(guī)范》JGJ1332001
- 防錯裝置檢查表
評論
0/150
提交評論