詳解在Java中如何創(chuàng)建多線程程序
創(chuàng)建多線程程序的第一種方式:創(chuàng)建Thread類的子類
java.lang.Thread類:是描述線程的類,我們想要實(shí)現(xiàn)多線程程序,就必須繼承Thread類
實(shí)現(xiàn)步驟:
1.創(chuàng)建一個(gè)Thread類的子類
2.在Thread類的子類中重寫Thread類中的run方法,設(shè)置線程任務(wù)(開啟線程要做什么?)
3.創(chuàng)建Thread類的子類對象
4.調(diào)用Thread類中的方法start方法,開啟新的線程,執(zhí)行run方法
void start()使該線程開始執(zhí)行;Java虛擬機(jī)調(diào)用該線程的run方法。
結(jié)果是兩個(gè)線程并發(fā)地運(yùn)行﹔當(dāng)前線程〈main線程〉和另一個(gè)線程〈創(chuàng)建的新線程,執(zhí)行其run方法)。
多次啟動一個(gè)線程是非法的(只能調(diào)用一次start方法)。特別是當(dāng)線程已經(jīng)結(jié)束執(zhí)行后,不能再重新啟動。
java程序?qū)儆趽屨际秸{(diào)度,那個(gè)線程的優(yōu)先級高,那個(gè)線程優(yōu)先執(zhí)行;同一個(gè)優(yōu)先級,隨機(jī)選擇一個(gè)執(zhí)行
public class threadex extends Thread{ public void run(){ for (int i = 0; i < 20; i++) { System.out.println("run"+i); } } }
public class thread { public static void main(String[] args) { threadex mt=new threadex(); mt.start(); for (int i = 0 ; i < 20; i++) { System.out.println("main"+i); } } }
最終結(jié)果是隨機(jī)的:
多線程隨機(jī)性原理:
創(chuàng)建多線程程序的第二種方式:實(shí)現(xiàn)RunnabLe接口
java.Lang.Runnable
Runnable接口應(yīng)該由那些打算通過某一線程執(zhí)行其實(shí)例的類來實(shí)現(xiàn)。類必須定義一個(gè)稱為run的無參數(shù)方法。
java.Lang.Thread類的構(gòu)造方法
Thread ( Runnable target)分配新的 Thread 對象。
Thread ( Runnable target, string name)分配新的Thread 對象。
實(shí)現(xiàn)步驟:
1.創(chuàng)建一個(gè)Runnable接口的實(shí)現(xiàn)類
2.在實(shí)現(xiàn)類中重寫Runnable接口的run方法,設(shè)置線程任務(wù)
3.創(chuàng)建一個(gè)Runnable接口的實(shí)現(xiàn)類對象
4.創(chuàng)建Thread類對象,構(gòu)造方法中傳遞Runnable接口的實(shí)現(xiàn)類對象
5.調(diào)用Thread類中的start方法,開啟新的線程執(zhí)行run方法
public class threadex2 implements Runnable{ @Override public void run() { for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName()+i); } } }
public class thread { public static void main(String[] args) { //第二種 threadex2 mt1=new threadex2(); Thread t=new Thread(mt1); t.start(); for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName()+i); } } }
結(jié)果同樣是隨機(jī)的:
實(shí)現(xiàn)Runnable接口創(chuàng)建多線程程序的好處:
1.避免了單繼承的局限性
一個(gè)類只能繼承一個(gè)類(一個(gè)人只能有一個(gè)親爹),類繼承了Thread類就不能繼承其他的類實(shí)現(xiàn)了Runnable接口,還可以繼承其他的類,實(shí)現(xiàn)其他的接口
2.增強(qiáng)了程序的擴(kuò)展性,降低了程序的耦合性(解耦)
實(shí)現(xiàn)Runnable接口的方式,把設(shè)置線程任務(wù)和開啟新線程進(jìn)行了分離(解耦)實(shí)現(xiàn)類中,重寫了run方法:用來設(shè)置線程任務(wù)
創(chuàng)建Thread類對象,調(diào)用start方法:用來開啟新線程
匿名內(nèi)部類方式實(shí)現(xiàn)線程的創(chuàng)建
匿名:沒有名字
內(nèi)部類:寫在其他類內(nèi)部的類
匿名內(nèi)部類作用:簡化代碼
把子類繼承父類,重寫父類的方法,創(chuàng)建子類對象合一步完成
把實(shí)現(xiàn)類實(shí)現(xiàn)接口,重寫接口中的方法,創(chuàng)建實(shí)現(xiàn)類對象合成一步完成
匿名內(nèi)部類的最終產(chǎn)物:子類/實(shí)現(xiàn)類對象,而這個(gè)類沒有名字
格式:
new 父類/接口 () {
重復(fù)父類/接口中的方法
};
public static void main(String[] args) { new Thread(){ public void run(){ for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName()+"cs"+i); } } }.start();//Thread-0 //線程的接口Runnable //Runnable r=new RunnableImpl();//多態(tài) Runnable r=new Runnable(){ //重寫run方法,設(shè)置線程任務(wù) public void run(){ for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName()+"ff"+i); } } }; new Thread(r).start(); //簡化 new Thread(new Runnable(){ //重寫run方法,設(shè)置線程任務(wù) public void run(){ for (int i = 0; i < 20; i++) { System.out.println(Thread.currentThread().getName()+"hj"+i); } } }).start(); }
Thread類中的常用方法:
獲取線程的名稱:
1.使用Thread類中的方法getName()
String getName() 返回該線程的名稱。
2.可以先獲取到當(dāng)前正在執(zhí)行的線程,使用線程中的方法getName()獲取線程的名稱
static Thread currentThread()返回對當(dāng)前正在執(zhí)行的線程對象的引用。
方法一
//方法一 public class threadfun extends Thread{ public void run(){ String name = getName(); System.out.println(name); } }
/* 線程的名稱: 主線程: main 新線程:Thread-0, Thread-1 , Thread-2 */ public class threadfunmain { public static void main(String[] args) { threadfun mt = new threadfun(); mt.start();//Thread-0 new threadfun().start();//Thread-1 new threadfun().start();//Thread-2 } }
方法二
public class threadfun extends Thread{ public void run(){ //方法二 Thread th = Thread.currentThread(); System.out.println(th); String name = getName(); System.out.println(name); //System.out.println(Thread.currentThread().getName()); } }
public class threadfunmain { public static void main(String[] args) { threadfun mt = new threadfun(); mt.start();//Thread[Thread-0,5,main] Thread-0 new threadfun().start();//Thread[Thread-1,5,main] Thread-1 new threadfun().start();//Thread[Thread-2,5,main] Thread-2 System.out.println(Thread.currentThread().getName());//main //主線程中只能用第二種方法獲取,因?yàn)闇y試類沒有繼承Thread類,沒有g(shù)etname方法 } }
設(shè)置線程的名稱:(了解)
1.使用Thread類中的方法setName(名字)
void setName ( String name)改變線程名稱,使之與參數(shù)name相同。
2.創(chuàng)建一個(gè)帶參數(shù)的構(gòu)造方法,參數(shù)傳遞線程的名稱;調(diào)用父類的帶參構(gòu)造方法,把線程名稱傳遞給父類,讓父類(Thread)給子線程起一個(gè)名字
Thread ( String name)分配新的Thread 對象。
public class threadfun extends Thread{ public void run(){ //設(shè)置線程的名稱方法一 System.out.println(Thread.currentThread().getName()); } //設(shè)置線程的名稱方法二 threadfun(){} threadfun(String name){ super(name); } }
public class threadfunmain { public static void main(String[] args) { threadfun mt = new threadfun(); //設(shè)置線程的名稱方法一 mt.setName("ess"); mt.start(); //設(shè)置線程的名稱方法二 new threadfun("ff").start(); } }
public static void sleep(Long millis):使當(dāng)前正在執(zhí)行的線程以指定的毫秒數(shù)暫停(暫時(shí)停止執(zhí)行)。毫秒數(shù)結(jié)束之后,線程繼續(xù)執(zhí)行
//sleep for (int i = 0; i < 20; i++) { System.out.println(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } }
到此這篇關(guān)于詳解在Java中如何創(chuàng)建多線程程序的文章就介紹到這了,更多相關(guān)Java創(chuàng)建多線程程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)有限狀態(tài)機(jī)的推薦方案分享
有限狀態(tài)機(jī)又稱有限狀態(tài)自動機(jī),簡稱狀態(tài)機(jī),是表示有限個(gè)狀態(tài)以及在這些狀態(tài)之間的轉(zhuǎn)移和動作等行為的數(shù)學(xué)模型,這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)有限狀態(tài)機(jī)的推薦方案,需要的朋友可以參考下2021-11-11Java CountDownLatch應(yīng)用場景代碼實(shí)例
這篇文章主要介紹了Java CountDownLatch應(yīng)用場景代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09關(guān)于MyBatis的foreach標(biāo)簽常用方法
這篇文章主要介紹了關(guān)于MyBatis的foreach標(biāo)簽常用方法,foreach 標(biāo)簽可以用來遍歷數(shù)組、列表和 Map 等集合參數(shù),實(shí)現(xiàn)批量操作或一些簡單 SQL 操作,需要的朋友可以參考下2023-05-05springboot?實(shí)現(xiàn)動態(tài)刷新配置的詳細(xì)過程
這篇文章主要介紹了springboot實(shí)現(xiàn)動態(tài)刷新配置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05使用Swagger2實(shí)現(xiàn)自動生成RESTful?API文檔
在開發(fā)?RESTful?API?的過程中,文檔是非常重要的一部分,可以幫助開發(fā)者了解?API?的功能和使用方法,本文將使用Swagger2?實(shí)現(xiàn)自動生成?RESTful?API?文檔,需要的可以參考一下2023-06-06簡單了解Spring Web相關(guān)模塊運(yùn)行原理
這篇文章主要介紹了簡單了解Spring Web相關(guān)模塊運(yùn)行原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Aop動態(tài)代理和cglib實(shí)現(xiàn)代碼詳解
這篇文章主要介紹了Aop動態(tài)代理和cglib實(shí)現(xiàn)代碼詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12springboot實(shí)現(xiàn)熱部署操作方法
這篇文章主要介紹了springboot實(shí)現(xiàn)熱部署操作方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11