java 多線程的三種構(gòu)建方法
java 多線程的三種構(gòu)建方法
繼承Thread類創(chuàng)建線程類
public class Thread extends Object implements Runnable
- 定義Thread類的子類,并重寫(xiě)其run()方法
- 創(chuàng)建Thread子類的實(shí)例,即創(chuàng)建了線程對(duì)象
- 調(diào)用線程對(duì)象的start()方法啟動(dòng)線程
public class FirstThread extends Thread { public void run(){ for(int i=0;i<100;i++){ /* * Thread類已經(jīng)繼承了Object * Object類創(chuàng)建了name選項(xiàng) 并且有其getName(),setName()方法 * 在繼承Thread的類里面使用時(shí)只需要用this引用 */ System.out.println(this.getName()+" "+i); } } public static void main(String[] args) { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); if(i==20){ new FirstThread().start(); new FirstThread().start(); } } } }
Thread類已經(jīng)繼承了Object
Object類創(chuàng)建了name選項(xiàng) 并且有其getName(),setName()方法
在繼承Thread的類里面使用時(shí)只需要用this引用
上面兩個(gè)副線程和主線程隨機(jī)切換,又因?yàn)槭褂玫氖抢^承Thread的類所以兩個(gè)副線程不能共享資源
start()方法調(diào)用后并不是立即執(zhí)行多線程代碼,而是使得該線程編程可運(yùn)行狀態(tài),什么時(shí)候運(yùn)行是由操作系統(tǒng)決定的
實(shí)現(xiàn)Runnable接口創(chuàng)建線程類
public Thread() public Thread(Runnable target) public Thread(Runnable target,String name)
- 定義Runnable接口的實(shí)現(xiàn)類,并重寫(xiě)該接口的run()方法
- 創(chuàng)建Runnable實(shí)現(xiàn)類的實(shí)例,并以此作為Thread的target來(lái)創(chuàng)建Thread對(duì)象,該Thread對(duì)象才是真正的線程對(duì)象。
public class SecondThread implements Runnable { public void run(){ for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); } } public static void main(String[] args) { for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); if(i==20){ SecondThread st=new SecondThread(); //通過(guò)new Thread(target,name)創(chuàng)建線程 new Thread(st,"新線程1").start(); new Thread(st,"新線程2").start(); } } } }
上面的結(jié)果是兩個(gè)副線程和主線程隨機(jī)切換,但是并沒(méi)有共享資源,因?yàn)樗麄兏緵](méi)有能用來(lái)共享的資源。
start()方法調(diào)用后并不是立即執(zhí)行多線程代碼,而是使得該線程編程可運(yùn)行狀態(tài),什么時(shí)候運(yùn)行是由操作系統(tǒng)決定的
繼承Thread類和創(chuàng)建Runnable接口的共享資源詳解
在只有可以用來(lái)共享的資源時(shí)候,也就是同用一個(gè)實(shí)例化對(duì)象。兩個(gè)創(chuàng)建方式在共享資源時(shí)才會(huì)有所區(qū)別,否則它們都不會(huì)共享資源共享資源通常用private static 修飾符來(lái)修飾。
class Thread1 extends Thread{ private int count=5; private String name; public Thread1(String name) { this.name=name; } public void run() { for (int i = 0; i < 5; i++) { System.out.println(name + "運(yùn)行 count= " + count--); try { sleep((int) Math.random() * 10); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { Thread1 mTh1=new Thread1("A"); Thread1 mTh2=new Thread1("B"); mTh1.start(); mTh2.start(); } }
B運(yùn)行 count= 5 A運(yùn)行 count= 5 B運(yùn)行 count= 4 B運(yùn)行 count= 3 B運(yùn)行 count= 2 B運(yùn)行 count= 1 A運(yùn)行 count= 4 A運(yùn)行 count= 3 A運(yùn)行 count= 2 A運(yùn)行 count= 1
正是因?yàn)橛辛藀rivate int count=5;一句才有了共享資源,但這是繼承Thread類的子類,并不能共享資源
class Thread2 implements Runnable{ private int count=15; public void run() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + "運(yùn)行 count= " + count--); try { Thread.sleep((int) Math.random() * 10); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { Thread2 my = new Thread2(); new Thread(my, "C").start();//同一個(gè)mt,但是在Thread中就不可以,如果用同一個(gè)實(shí)例化對(duì)象mt,就會(huì)出現(xiàn)異常 new Thread(my, "D").start(); new Thread(my, "E").start(); } }
C運(yùn)行 count= 15 D運(yùn)行 count= 14 E運(yùn)行 count= 13 D運(yùn)行 count= 12 D運(yùn)行 count= 10 D運(yùn)行 count= 9 D運(yùn)行 count= 8 C運(yùn)行 count= 11 E運(yùn)行 count= 12 C運(yùn)行 count= 7 E運(yùn)行 count= 6 C運(yùn)行 count= 5 E運(yùn)行 count= 4 C運(yùn)行 count= 3 E運(yùn)行 count= 2
同樣的正是因?yàn)橛辛藀rivate int count=15這個(gè)共同的實(shí)例化對(duì)象,實(shí)現(xiàn)Runnable的類才可以共享資源
那么為什么繼承Thread類的子類實(shí)現(xiàn)Runable接口的類在共享資源時(shí)有區(qū)別呢?
因?yàn)镴ava中只能支持單繼承,單繼承特點(diǎn)意味著只能有一個(gè)子類去繼承 而Runnabl接口后可以跟好多類,便可以進(jìn)行多個(gè)線程共享一個(gè)資源的操作
使用Callable和Future創(chuàng)建線程
Callable怎么看起來(lái)都像Runnable接口的增強(qiáng)版,Callable有一個(gè)call()方法相當(dāng)于Runnable的run()方法,但是功能卻更加強(qiáng)大:
call()方法可以有返回值
call()方法可以聲明拋出異常
Callable接口有泛型限制,Callable接口里的泛型形參類型與call()方法的返回值類型相同。 而且Callable接口是函數(shù)式接口,因此可使用Lambda表達(dá)式創(chuàng)建Callable對(duì)象 Runnable接口也是函數(shù)式接口,因此也可以使用Lambda表達(dá)式創(chuàng)建Runnable對(duì)象
- 創(chuàng)建Callable接口的實(shí)現(xiàn)類,并實(shí)現(xiàn)call()方法,該call()方法將作為線程執(zhí)行體,再創(chuàng)建Callable實(shí)現(xiàn)類的實(shí)例
- 使用FutureTask類來(lái)包裝Callable對(duì)象,該FutureTask對(duì)象封裝了該Callable對(duì)象的call()方法
- 使用FutureTask類對(duì)象作為Thread對(duì)象的target創(chuàng)建并啟動(dòng)新線程
- 調(diào)用FutureTask對(duì)象的get()方法來(lái)獲得子線程結(jié)束后的返回值
public class ThirdThread implements Callable<Integer> { public Integer call(){ int i=0; for(;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); } return i; } public static void main(String[] args){ ThirdThread tt=new ThirdThread(); FutureTask<Integer> task=new FutureTask<>(tt); Thread t=new Thread(task,"有返回值的線程"); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+" "+i); if(i==20){ t.start(); } } try{ System.out.println("返回值是:"+task.get()); }catch(Exception e){ e.printStackTrace(); } } }
使用Lambda表達(dá)式的Callable和Future創(chuàng)建的線程
public class ThirdThread{ public static void main(String[] args){ ThirdThread tt=new ThirdThread(); //先使用Lambda表達(dá)式創(chuàng)建Callable<Integer>對(duì)象 //使用FutureTask封裝Callable對(duì)象 FutureTask<Integer> task=new FutureTask<Integer>((Callable<Integer>)()->{ int i=0; for(;i<100;i++){ System.out.println(Thread.currentThread().getName()+"的循環(huán)變量i的值:"+i); } return i; }); for(int i=0;i<100;i++){ System.out.println(Thread.currentThread().getName()+"的循環(huán)變量i的值:"+i); if(i==20){ new Thread(task,"有返回值的線程").start(); } } try{ System.out.println("子線程的返回值"+task.get()); }catch(Exception e){ e.printStackTrace(); } } }
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- java 中多線程生產(chǎn)者消費(fèi)者問(wèn)題詳細(xì)介紹
- java多線程編程學(xué)習(xí)(線程間通信)
- Java多線程的用法詳細(xì)介紹
- java多線程學(xué)習(xí)筆記之自定義線程池
- Java基于Socket實(shí)現(xiàn)簡(jiǎn)單的多線程回顯服務(wù)器功能示例
- java 多線程Thread與runnable的區(qū)別
- java多線程學(xué)習(xí)之死鎖的模擬和避免(實(shí)例講解)
- java多線程之火車售票系統(tǒng)模擬實(shí)例
- Java Socket實(shí)現(xiàn)多線程通信功能示例
- Java多線程用法的實(shí)例詳解
相關(guān)文章
IDEA整合Dubbo+Zookeeper+SpringBoot實(shí)現(xiàn)
初學(xué)者,想自己動(dòng)手做一個(gè)簡(jiǎn)單的demo,本文主要介紹了IDEA整合Dubbo+Zookeeper+SpringBoot實(shí)現(xiàn),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06通過(guò)實(shí)例解析POJO和JavaBean的區(qū)別
這篇文章主要介紹了通過(guò)實(shí)例解析POJO和JavaBean的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07springboot如何忽略接收請(qǐng)求中的參數(shù)
這篇文章主要介紹了springboot如何忽略接收請(qǐng)求中的參數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07Java設(shè)計(jì)模式開(kāi)發(fā)中使用觀察者模式的實(shí)例教程
這篇文章主要介紹了Java設(shè)計(jì)模式開(kāi)發(fā)中使用觀察者模式的實(shí)例教程,松耦合和邏輯清晰的消息監(jiān)聽(tīng)是觀察者模式的大特色,需要的朋友可以參考下2016-04-04用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字
這篇文章主要介紹了用Java產(chǎn)生100個(gè)1-150間不重復(fù)數(shù)字,需要的朋友可以參考下2017-02-02淺談Java中的atomic包實(shí)現(xiàn)原理及應(yīng)用
這篇文章主要介紹了淺談Java中的atomic包實(shí)現(xiàn)原理及應(yīng)用,涉及Atomic在硬件上的支持,Atomic包簡(jiǎn)介及源碼分析等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Mybatis-Plus自動(dòng)填充更新操作相關(guān)字段的實(shí)現(xiàn)
數(shù)據(jù)庫(kù)表中應(yīng)該都要有create_time、update_time字段;那么在開(kāi)發(fā)中,對(duì)于這些共有字段的處理應(yīng)該要進(jìn)行統(tǒng)一,這樣就可以簡(jiǎn)化我們的開(kāi)發(fā)過(guò)程。那么本文就對(duì)Mybatis-Plus中的字段自動(dòng)填充進(jìn)行記錄2021-11-11Java異常 Factory method''sqlSessionFactory''rew exception;este
這篇文章主要介紹了Java異常 Factory method ‘sqlSessionFactory‘ threw exception; nested exception is java.lang.NoSuchMethodError:,本文介紹了springboot 引入mybatis-plus后報(bào)錯(cuò)的解決方案,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07Java數(shù)組創(chuàng)建的3種方法6種寫(xiě)法代碼示例
這篇文章主要給大家介紹了關(guān)于Java數(shù)組創(chuàng)建的3種方法6種寫(xiě)法,在Java中我們可以使用關(guān)鍵字new來(lái)創(chuàng)建一個(gè)數(shù)組,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01