Java中Thread和Runnable創(chuàng)建線(xiàn)程的方式對(duì)比
一、通過(guò)繼承Thread創(chuàng)建線(xiàn)程
通過(guò)繼承Thread類(lèi),創(chuàng)建一個(gè)線(xiàn)程,在主線(xiàn)程中,調(diào)用start,讓線(xiàn)程處于runnable狀態(tài),讓系統(tǒng)去運(yùn)行線(xiàn)程的方法。
public class MyThread extends Thread { @Override public void run() { System.out.println("執(zhí)行線(xiàn)程"); } } public class ThreadTest { public static void main(String[] args) { Thread thread = new MyThread(); thread.start(); } }
二、實(shí)現(xiàn)Runnable創(chuàng)建檢查
通過(guò)實(shí)現(xiàn)runnable接口,創(chuàng)建一個(gè)線(xiàn)程,需要把runnable的實(shí)現(xiàn)類(lèi),作為T(mén)hread的構(gòu)造方法的參數(shù),運(yùn)行這個(gè)線(xiàn)程。
public class MyThread implements Runnable { @Override public void run() { System.out.println("執(zhí)行線(xiàn)程"); } } public class ThreadTest { public static void main(String[] args) { Runnable thread = new MyThread(); Thread thread1 = new Thread(thread); thread1.start(); } }
這里要注意一些,runnable的實(shí)現(xiàn)類(lèi),是沒(méi)有start()方法的,只有run()方法
如果運(yùn)行這個(gè)run方法,依然能運(yùn)行線(xiàn)程的方法,但是,這個(gè)方法并不是在新的線(xiàn)程中運(yùn)行的,而是在主線(xiàn)程中運(yùn)行的。
public class MyThread implements Runnable { @Override public void run() { System.out.println("此方法在:"+Thread.currentThread().getName()+"中運(yùn)行"); } } public class ThreadTest { public static void main(String[] args) { Runnable run = new MyThread(); System.out.println("此方法在:"+Thread.currentThread().getName()+"中執(zhí)行"); run.run(); // Thread thread1 = new Thread(run); // thread1.start(); } }
運(yùn)行結(jié)果:
如果把注釋打開(kāi),run()注釋掉,結(jié)果如下:
三、比較兩種創(chuàng)建方式
3.1、多繼承
Java是不支持類(lèi)的多繼承的,但是支持接口的多實(shí)現(xiàn),所以Runnable在使用上更加靈活。
3.2、 數(shù)據(jù)共享
如果一個(gè)類(lèi)繼承Thread,則不適合資源共享。但是如果實(shí)現(xiàn)了Runable接口的話(huà),則很容易的實(shí)現(xiàn)資源共享。
如果線(xiàn)程中有一個(gè)成員變量,使用Runnable創(chuàng)建的線(xiàn)程,多個(gè)線(xiàn)程之間可以共同訪(fǎng)問(wèn)這個(gè)變量,而Thread不可以。
為什么呢?一個(gè)Thread創(chuàng)建的線(xiàn)程,每次都是new的一個(gè)新的線(xiàn)程對(duì)象,線(xiàn)程對(duì)象之間的成員變量不共享。而Runnale,可以作為T(mén)hread類(lèi)的參數(shù),可以多創(chuàng)建幾個(gè)Thread類(lèi),把Runnale作為參數(shù),讓多個(gè)線(xiàn)程一起運(yùn)行。
public class ThreadTest { public static void main(String[] args) { Runnable run = new MyThread(); Thread thread1 = new Thread(run); Thread thread2 = new Thread(run); Thread thread3 = new Thread(run); thread1.start(); thread2.start(); thread3.start(); } }
3.3、線(xiàn)程池
線(xiàn)程池只能放入實(shí)現(xiàn)Runable或callable類(lèi)線(xiàn)程,不能直接放入繼承Thread的類(lèi)。
四、源碼分析
當(dāng)我們進(jìn)入Thread方法,可以看的Thread也實(shí)現(xiàn)了Runnable接口
使用Thread肯定也實(shí)現(xiàn)了他的run方法。
可以看出,在這個(gè)run方法中,如果target不為空,執(zhí)行target的run方法,那target是什么?
進(jìn)入后,發(fā)現(xiàn)是runnable接口,那它是如何初始化的?突然想起,執(zhí)行runnable的實(shí)現(xiàn)類(lèi)時(shí),都會(huì)把他通過(guò)構(gòu)造函數(shù)傳遞,那來(lái)看看構(gòu)造函數(shù)。
繼續(xù)往下,可以看出確實(shí)是在這里初始化的。
所以,Runnbale接口的run方法,是通過(guò)Thread的成員變量去執(zhí)行的。
到此這篇關(guān)于Java中Thread和Runnable創(chuàng)建線(xiàn)程的方式對(duì)比的文章就介紹到這了,更多相關(guān)Java中Thread和Runnable創(chuàng)建線(xiàn)程內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信)
這篇文章主要介紹了java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10關(guān)于java的九個(gè)預(yù)定義Class對(duì)象
這篇文章主要介紹了關(guān)于java的九個(gè)預(yù)定義Class對(duì)象,在Java中,沒(méi)有類(lèi)就無(wú)法做任何事情。然而,并不是所有的類(lèi)都具有面向?qū)ο筇卣?。如Math.random,并只需要知道方法名和參數(shù),需要的朋友可以參考下2023-05-05servlet監(jiān)聽(tīng)器的學(xué)習(xí)使用(三)
這篇文章主要為大家詳細(xì)介紹了servlet監(jiān)聽(tīng)器學(xué)習(xí)使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09SpringBoot Actuator監(jiān)控的項(xiàng)目實(shí)踐
本文主要結(jié)合 Spring Boot Actuator,跟大家一起分享微服務(wù)Spring Boot Actuator 的常見(jiàn)用法,方便我們?cè)谌粘V袑?duì)我們的微服務(wù)進(jìn)行監(jiān)控治理,感興趣的可以了解一下2024-01-01JavaMail實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送
這篇文章主要為大家詳細(xì)介紹了JavaMail實(shí)現(xiàn)簡(jiǎn)單郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08ireport數(shù)據(jù)表格報(bào)表的簡(jiǎn)單使用
這篇文章給大家介紹了如何畫(huà)一個(gè)報(bào)表模板,這里介紹下畫(huà)表格需要用到的組件,文中通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-10-10