Java中啟動線程start和run的兩種方法
一、區(qū)別
Java中啟動線程有兩種方法,繼承Thread類和實現(xiàn)Runnable接口,由于Java無法實現(xiàn)多重繼承,所以一般通過實現(xiàn)Runnable接口來創(chuàng)建線程。但是無論哪種方法都可以通過start()和run()方法來啟動線程,下面就來介紹一下他們的區(qū)別。
start方法:
通過該方法啟動線程的同時也創(chuàng)建了一個線程,真正實現(xiàn)了多線程。無需等待run()方法中的代碼執(zhí)行完畢,就可以接著執(zhí)行下面的代碼。此時start()的這個線程處于就緒狀態(tài),當?shù)玫紺PU的時間片后就會執(zhí)行其中的run()方法。這個run()方法包含了要執(zhí)行的這個線程的內(nèi)容,run()方法運行結束,此線程也就終止了。
run方法:
通過run方法啟動線程其實就是調用一個類中的方法,當作普通的方法的方式調用。并沒有創(chuàng)建一個線程,程序中依舊只有一個主線程,必須等到run()方法里面的代碼執(zhí)行完畢,才會繼續(xù)執(zhí)行下面的代碼,這樣就沒有達到寫線程的目的。
下面我們通過一個很經(jīng)典的題目來理解一下:
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"); } }
代碼如圖所示,那么運行程序,輸出的應該是什么呢?沒錯,輸出的是”pong ping”。因為t.run()實際上就是等待執(zhí)行new Thread里面的run()方法調用pong()完畢后,再繼續(xù)打印”ping”。它不是真正的線程。
而如果我們將t.run();修改為t.start();那么,結果很明顯就是”ping pong”,因為當執(zhí)行到此處,創(chuàng)建了一個新的線程t并處于就緒狀態(tài),代碼繼續(xù)執(zhí)行,打印出”ping”。此時,執(zhí)行完畢。線程t得到CPU的時間片,開始執(zhí)行,調用pong()方法打印出”pong”。
如果感興趣,還可以多加幾條語句自己看看效果。
二、源碼
那么他們本質上的區(qū)別在哪里,我們來看一下源碼:
/**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();
可以看到,當一個線程啟動的時候,它的狀態(tài)(threadStatus)被設置為0,如果不為0,則拋出IllegalThreadStateException異常。正常的話,將該線程加入線程組,最后嘗試調用start0方法,而start0方法是私有的native方法(Native Method是一個java調用非java代碼的接口)。
我猜測這里是用C實現(xiàn)的,看來調用系統(tǒng)底層還是要通過C語言。這也就是為什么start()方法可以實現(xiàn)多線程。而調用run()方法,其實只是調用runnable里面自己實現(xiàn)的run()方法。
我們再看看Thread里run()的源碼:
@Override public void run() { if (target != null) { target.run(); } }
如果target不為空,則調用target的run()方法,那么target是什么:
/* What will be run. */ private Runnable target;
其實就是一個Runnable接口,正如上面代碼中new Thread的部分,其實我們就是在實現(xiàn)它的run()方法。所以如果直接調用run,就和一個普通的方法沒什么區(qū)別,是不會創(chuàng)建新的線程的,因為壓根就沒執(zhí)行start0方法。
三、實現(xiàn)
前面說了,繼承Thread類和實現(xiàn)Runnable接口都可以定義一個線程,那么他們又有什么區(qū)別呢?
在《Java核心技術卷1 第9版》第627頁提到??梢酝ㄟ^一下代碼構建Thread的子類定義一個線程:
class MyThread extends Thread { public void run() { //do Something } }
然后,實例化一個對象,調用其start方法。不過這個方法不推薦。應該減少需要并行運行的任務數(shù)量。如果任務很多,要為每個任務創(chuàng)建一個獨立的線程所付出的代價太多,當然可以用線程池來解決。
實現(xiàn)Runnable接口所具有的優(yōu)勢:
1、避免Java單繼承的問題
2、適合多線程處理同一資源
3、代碼可以被多線程共享,數(shù)據(jù)獨立,很容易實現(xiàn)資源共享
總結一下:
1.start() 可以啟動一個新線程,run()不能
2.start()不能被重復調用,run()可以
3.start()中的run代碼可以不執(zhí)行完就繼續(xù)執(zhí)行下面的代碼,即進行了線程切換。直接調用run方法必須等待其代碼全部執(zhí)行完才能繼續(xù)執(zhí)行下面的代碼。
4.start() 實現(xiàn)了多線程,run()沒有實現(xiàn)多線程。
以上所述是小編給大家介紹的Java中啟動線程start和run方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關文章
Spring整合Quartz Job以及Spring Task的實現(xiàn)方法
下面小編就為大家分享一篇Spring整合Quartz Job以及Spring Task的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12SpringBoot+微信小程序實現(xiàn)文件上傳與下載功能詳解
這篇文章主要為大家介紹了SpringBoot整合微信小程序實現(xiàn)文件上傳與下載功能,文中的實現(xiàn)步驟講解詳細,快跟隨小編一起學習一下吧2022-03-03maven的settings.xml、pom.xml配置文件使用詳解
本文詳解了Maven中的配置文件settings.xml和pom.xml,闡述了它們的作用、配置項以及優(yōu)先級順序,settings.xml存在于Maven安裝目錄和用戶目錄下,分別作用于全局和當前用戶,pom.xml位于項目根路徑下2024-09-09