多線程的使用_第1頁
多線程的使用_第2頁
多線程的使用_第3頁
多線程的使用_第4頁
多線程的使用_第5頁
已閱讀5頁,還剩9頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

多線程的使用目錄多線程的概念多線程的使用方法多線程使用實例小結(jié)多線程的概念什么是多線程?多線程(multi-thread)是指在單個的程序內(nèi)可以同時運(yùn)行多個不同的線程完成不同的任務(wù)一個線程兩個線程多線程的概念為什么使用多線程?現(xiàn)實世界中1條車道和4條車道哪個車流量大?打掃房間是一個人打掃快還是多個人打掃快?賣火車票時一個窗口和多個窗口開賣,哪種方式賣得快并行執(zhí)行效率高多線程的使用方法如何使用線程?創(chuàng)建線程(主要有兩種方法)方法一:繼承Thread類,重寫run()方法方法二:實現(xiàn)Runnable接口,并實現(xiàn)該接口的run()方法啟動線程main方法是程序的入口,main方法的代碼都是在java默認(rèn)創(chuàng)建的一條主線程里運(yùn)行的。main方法線程所運(yùn)行的代碼是main方法中定義的,那么我們自定義創(chuàng)建的線程,線程所執(zhí)行的代碼是run()方法里定義的自定義的線程也是依托于主線程的,意味著即使我們寫了新線程,也需要在主線程中啟動它創(chuàng)建和啟動線程-繼承Thread類,重寫run()方法創(chuàng)建一個Thread類的子類創(chuàng)建一個類對象啟動線程多線程的使用方法publicclassSubThreadextendsThread{publicvoidrun(){…}}SubThreadt01=newSubThread();t01.start();創(chuàng)建和啟動線程-繼承Thread類繼承Thread類,重寫run()方法publicclassSubThreadextendsThread{ publicstaticvoidmain(String[]args){ SubThreadt01=newSubThread();SubThreadt02=newSubThread(); t01.start();t02.start();}publicvoidrun(){ for(inti=0;i<100;i++){System.out.println(Thread.currentThread().getName());} }}多線程的使用方法創(chuàng)建和啟動線程-實現(xiàn)Runnable接口,實現(xiàn)該接口的run()方法創(chuàng)建一個實現(xiàn)Runnable接口的子類創(chuàng)建一個類對象由Runnable創(chuàng)建一個Thread對象啟動線程publicclassSubRunnableimplementsRunnable{publicvoidrun(){…}}SubRunnabler=newSubRunnable();Threadt=newThread(r);t.start();多線程的使用方法

實現(xiàn)Runnable接口,并實現(xiàn)該接口的run()方法publicclassSubRunnableimplementsRunnable{publicstaticvoidmain(String[]args){Runnable實現(xiàn)的多線程。

SubRunnabler=newSubRunnable();newThread(r).start();newThread(r).start();}publicvoidrun(){ for(inti=0;i<100;i++){ System.out.println(Thread.currentThread().getName());} }}多線程的使用方法示例:火車票售賣窗口完成多窗口售賣火車票的案例,比較兩種線程創(chuàng)建的異同。使用繼承Thread方法publicclassT02_extendsThreadextendsThread{

intticket=100; publicstaticvoidmain(String[]args){ T02_extendsThreadt01=newT02_extendsThread(); T02_extendsThreadt02=newT02_extendsThread(); t01.start(); t02.start();} publicvoidrun(){ while(ticket>=1){ try{ Thread.sleep(50); }catch(InterruptedExceptione){e.printStackTrace();} System.out.println("窗口"+Thread.currentThread().getName()+":賣出第"+ticket+"張車票"); ticket--;} }}示例:火車票售賣窗口使用實現(xiàn)Runnable方法publicclassT03_implementsRunnableimplementsRunnable{

intticket=100; publicstaticvoidmain(String[]args){

T03_implementsRunnabler=newT03_implementsRunnable(); newThread(r).start(); newThread(r).start();} publicvoidrun(){ while(ticket>=1){ try{ Thread.sleep(50); }catch(InterruptedExceptione){e.printStackTrace();} Sy

溫馨提示

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

最新文檔

評論

0/150

提交評論