Java中啟動(dòng)線程start和run的兩種方法
一、區(qū)別
Java中啟動(dòng)線程有兩種方法,繼承Thread類和實(shí)現(xiàn)Runnable接口,由于Java無(wú)法實(shí)現(xiàn)多重繼承,所以一般通過(guò)實(shí)現(xiàn)Runnable接口來(lái)創(chuàng)建線程。但是無(wú)論哪種方法都可以通過(guò)start()和run()方法來(lái)啟動(dòng)線程,下面就來(lái)介紹一下他們的區(qū)別。
start方法:
通過(guò)該方法啟動(dòng)線程的同時(shí)也創(chuàng)建了一個(gè)線程,真正實(shí)現(xiàn)了多線程。無(wú)需等待run()方法中的代碼執(zhí)行完畢,就可以接著執(zhí)行下面的代碼。此時(shí)start()的這個(gè)線程處于就緒狀態(tài),當(dāng)?shù)玫紺PU的時(shí)間片后就會(huì)執(zhí)行其中的run()方法。這個(gè)run()方法包含了要執(zhí)行的這個(gè)線程的內(nèi)容,run()方法運(yùn)行結(jié)束,此線程也就終止了。
run方法:
通過(guò)run方法啟動(dòng)線程其實(shí)就是調(diào)用一個(gè)類中的方法,當(dāng)作普通的方法的方式調(diào)用。并沒(méi)有創(chuàng)建一個(gè)線程,程序中依舊只有一個(gè)主線程,必須等到run()方法里面的代碼執(zhí)行完畢,才會(huì)繼續(xù)執(zhí)行下面的代碼,這樣就沒(méi)有達(dá)到寫(xiě)線程的目的。
下面我們通過(guò)一個(gè)很經(jīng)典的題目來(lái)理解一下:
public class Test { public static void main(String[] args) { Thread t = new Thread(){ public void run() { pong(); } }; t.run(); System.out.println("ping"); } static void pong() { System.out.println("pong"); } }
代碼如圖所示,那么運(yùn)行程序,輸出的應(yīng)該是什么呢?沒(méi)錯(cuò),輸出的是”pong ping”。因?yàn)閠.run()實(shí)際上就是等待執(zhí)行new Thread里面的run()方法調(diào)用pong()完畢后,再繼續(xù)打印”ping”。它不是真正的線程。
而如果我們將t.run();修改為t.start();那么,結(jié)果很明顯就是”ping pong”,因?yàn)楫?dāng)執(zhí)行到此處,創(chuàng)建了一個(gè)新的線程t并處于就緒狀態(tài),代碼繼續(xù)執(zhí)行,打印出”ping”。此時(shí),執(zhí)行完畢。線程t得到CPU的時(shí)間片,開(kāi)始執(zhí)行,調(diào)用pong()方法打印出”pong”。
如果感興趣,還可以多加幾條語(yǔ)句自己看看效果。
二、源碼
那么他們本質(zhì)上的區(qū)別在哪里,我們來(lái)看一下源碼:
/**java * Causes this thread to begin execution; the Java Virtual Machine * calls the <code>run</code> method of this thread. * <p> * The result is that two threads are running concurrently: the * current thread (which returns from the call to the * <code>start</code> method) and the other thread (which executes its * <code>run</code> method). * <p> * It is never legal to start a thread more than once. * In particular, a thread may not be restarted once it has completed * execution. * * @exception IllegalThreadStateException if the thread was already * started. * @see #run() * @see #stop() */ public synchronized void start() { /** * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * * A zero status value corresponds to state "NEW". */ if (threadStatus != 0) throw new IllegalThreadStateException(); /* Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. */ group.add(this); boolean started = false; try { start0(); started = true; } finally { try { if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { /* do nothing. If start0 threw a Throwable then it will be passed up the call stack */ } } } private native void start0();
可以看到,當(dāng)一個(gè)線程啟動(dòng)的時(shí)候,它的狀態(tài)(threadStatus)被設(shè)置為0,如果不為0,則拋出IllegalThreadStateException異常。正常的話,將該線程加入線程組,最后嘗試調(diào)用start0方法,而start0方法是私有的native方法(Native Method是一個(gè)java調(diào)用非java代碼的接口)。
我猜測(cè)這里是用C實(shí)現(xiàn)的,看來(lái)調(diào)用系統(tǒng)底層還是要通過(guò)C語(yǔ)言。這也就是為什么start()方法可以實(shí)現(xiàn)多線程。而調(diào)用run()方法,其實(shí)只是調(diào)用runnable里面自己實(shí)現(xiàn)的run()方法。
我們?cè)倏纯碩hread里run()的源碼:
@Override public void run() { if (target != null) { target.run(); } }
如果target不為空,則調(diào)用target的run()方法,那么target是什么:
/* What will be run. */ private Runnable target;
其實(shí)就是一個(gè)Runnable接口,正如上面代碼中new Thread的部分,其實(shí)我們就是在實(shí)現(xiàn)它的run()方法。所以如果直接調(diào)用run,就和一個(gè)普通的方法沒(méi)什么區(qū)別,是不會(huì)創(chuàng)建新的線程的,因?yàn)閴焊蜎](méi)執(zhí)行start0方法。
三、實(shí)現(xiàn)
前面說(shuō)了,繼承Thread類和實(shí)現(xiàn)Runnable接口都可以定義一個(gè)線程,那么他們又有什么區(qū)別呢?
在《Java核心技術(shù)卷1 第9版》第627頁(yè)提到??梢酝ㄟ^(guò)一下代碼構(gòu)建Thread的子類定義一個(gè)線程:
class MyThread extends Thread { public void run() { //do Something } }
然后,實(shí)例化一個(gè)對(duì)象,調(diào)用其start方法。不過(guò)這個(gè)方法不推薦。應(yīng)該減少需要并行運(yùn)行的任務(wù)數(shù)量。如果任務(wù)很多,要為每個(gè)任務(wù)創(chuàng)建一個(gè)獨(dú)立的線程所付出的代價(jià)太多,當(dāng)然可以用線程池來(lái)解決。
實(shí)現(xiàn)Runnable接口所具有的優(yōu)勢(shì):
1、避免Java單繼承的問(wèn)題
2、適合多線程處理同一資源
3、代碼可以被多線程共享,數(shù)據(jù)獨(dú)立,很容易實(shí)現(xiàn)資源共享
總結(jié)一下:
1.start() 可以啟動(dòng)一個(gè)新線程,run()不能
2.start()不能被重復(fù)調(diào)用,run()可以
3.start()中的run代碼可以不執(zhí)行完就繼續(xù)執(zhí)行下面的代碼,即進(jìn)行了線程切換。直接調(diào)用run方法必須等待其代碼全部執(zhí)行完才能繼續(xù)執(zhí)行下面的代碼。
4.start() 實(shí)現(xiàn)了多線程,run()沒(méi)有實(shí)現(xiàn)多線程。
以上所述是小編給大家介紹的Java中啟動(dòng)線程start和run方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
java鎖synchronized面試常問(wèn)總結(jié)
這篇文章主要介紹了java鎖synchronized面試常問(wèn)總結(jié)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12使用Spring?AOP實(shí)現(xiàn)用戶操作日志功能
這篇文章主要介紹了使用Spring?AOP實(shí)現(xiàn)了用戶操作日志功能,功能實(shí)現(xiàn)需要一張記錄日志的log表,結(jié)合示例代碼給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Spring整合Quartz Job以及Spring Task的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12JavaWeb驗(yàn)證碼校驗(yàn)功能代碼實(shí)例
這篇文章主要介紹了JavaWeb驗(yàn)證碼校驗(yàn)功能代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04SpringBoot+微信小程序?qū)崿F(xiàn)文件上傳與下載功能詳解
這篇文章主要為大家介紹了SpringBoot整合微信小程序?qū)崿F(xiàn)文件上傳與下載功能,文中的實(shí)現(xiàn)步驟講解詳細(xì),快跟隨小編一起學(xué)習(xí)一下吧2022-03-03maven的settings.xml、pom.xml配置文件使用詳解
本文詳解了Maven中的配置文件settings.xml和pom.xml,闡述了它們的作用、配置項(xiàng)以及優(yōu)先級(jí)順序,settings.xml存在于Maven安裝目錄和用戶目錄下,分別作用于全局和當(dāng)前用戶,pom.xml位于項(xiàng)目根路徑下2024-09-09