




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、1 線程的概1 線程的概 AB多線程的弊端AB多線程的弊端CPU開銷。線程的使用會給系統帶來上下文切換的額外負2 創建線程的2 創建線程的方2.1 Thread,runrun,runrun,run()JVN不會當作線程來運行,會認為是普通的方Thread類的對象并啟動該線程,但是如果沒有重寫run(),什么也不2.2 Thread類的對象并啟動該線程,但是如果沒有重寫run(),什么也不2.2 線程池中,變得可運行,等待獲取cpu 的執行權。 阻臨時塞: CPU 使用權,暫時停止運行。直到線程進入2.3 Thread(String name) setName(Stringname) newPr
2、iority) 現取決于底層的操作系統的實現(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設置線程優先級 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的實現類的對象就是這個工人的工作(通過Thread類可以理解為一個工人,Runnable的實現類的對象就是這個工人的工作(通過RunnableThread的構造時,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中的每個對象都有一個內置鎖,只有當對象具有同步方法代碼時,內置鎖才會起56sleep() 4 死5 2T1L12T1L1T2 L2T1 L2T2 在等待對publicclasspublicclassDeadLockpublicicvoidmain(Stringargs)newThread(newRunnable(創建線程publicvoidrun()synchronized(刀叉)你不給我筷子我就不給你刀叉try catch (erruptedExceptione) synchronized(筷子.getName(給你刀叉,newT
7、hread(newRunnable(publicvoidrun()synchronized(筷子)你先給我刀叉我再給你筷子try catch (erruptedExceptione) synchronized(刀叉.getName(好吧把筷子給你人5 線程的通whilenotifyAll喚醒,notify容易出現只喚醒本方線程情況,導致程序中的所有線程都在等待。例5 線程的通whilenotifyAll喚醒,notify容易出現只喚醒本方線程情況,導致程序中的所有線程都在等待。例空和 的publicclassDemo10public sicvoidmain(Stringargs) p = ne
8、wro = new ProducConsumer con = new ConsumThread t1 = new Threro生產者); 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生產者); 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()有獲得執行權,并持有了相同的監視器(鎖)并調用 notify 為止。 notify:喚醒持有同一個監視器(鎖)wait的第一個線程,例如,餐館有空位置后,等候就餐最久的顧客最先入座。注意:被喚醒的線程是進入了可運行狀態。等待 cpu?packagepublicclassDemo10public sicvoidm
13、ain(Stringargs) p = newro = new ProducConsumercon=newThreadt1newro生產者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) 上述案例中定義了一個計數器 i,用來控制 main方法(主線程)的循環打印次數,在 i上述案例中定義了一個計數器 i,用來控制 main方法(主線程)的循環打印次數,在 i到 終止了線程1whil
16、e循環,run方法1也隨之結束。注意:當計i變為 線當所有的非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線程執行,onemian線程中,o
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 倉儲主管崗位面試問題及答案
- 病案管理員崗位面試問題及答案
- 珠寶私人定制設計師崗位面試問題及答案
- 2025屆浙江溫州十五校聯盟高二下化學期末質量跟蹤監視模擬試題含解析
- 廣東佛山市禪城區2025年化學高一下期末復習檢測模擬試題含解析
- 內蒙古阿拉善2025年高二化學第二學期期末學業質量監測試題含解析
- 民航誠信管理暫行辦法
- 協定稅率貨物管理辦法
- 廈門翔安垃圾管理辦法
- 我國非營利組織公信力建設的現實問題與改進策略
- 夫妻婚內財產約定協議書范本(2025年)
- 2025年中醫師承合同范本文字
- 0-3歲嬰幼兒感覺統合訓練知到智慧樹章節測試課后答案2024年秋杭州師范大學
- 抑酸藥課件教學課件
- 2024譯林版七年級英語上冊單詞(帶音標)
- 北師大版數學六年級上學期期中試卷
- 新生兒重癥監護室母乳使用專家共識(2024版)解讀
- 病毒性腦炎診療指南(兒科)
- 樂器設備供貨項目實施方案及售后服務方案
- 中共黨史知識競賽試題及答案
- 2020年杭州學軍中學高一入學分班考試英語試卷及答案
評論
0/150
提交評論