一、區(qū)別
Java中啟動(dòng)線程有兩種方法,繼承Thread類和實(shí)現(xiàn)Runnable接口,由于Java無法實(shí)現(xiàn)多重繼承,所以一般通過實(shí)現(xiàn)Runnable接口來創(chuàng)建線程。但是無論哪種方法都可以通過start()和run()方法來啟動(dòng)線程,下面就來介紹一下他們的區(qū)別。
start方法:
通過該方法啟動(dòng)線程的同時(shí)也創(chuàng)建了一個(gè)線程,真正實(shí)現(xiàn)了多線程。無需等待run()方法中的代碼執(zhí)行完畢,就可以接著執(zhí)行下面的代碼。此時(shí)start()的這個(gè)線程處于就緒狀態(tài),當(dāng)?shù)玫紺PU的時(shí)間片后就會執(zhí)行其中的run()方法。這個(gè)run()方法包含了要執(zhí)行的這個(gè)線程的內(nèi)容,run()方法運(yùn)行結(jié)束,此線程也就終止了。
run方法:
通過run方法啟動(dòng)線程其實(shí)就是調(diào)用一個(gè)類中的方法,當(dāng)作普通的方法的方式調(diào)用。并沒有創(chuàng)建一個(gè)線程,程序中依舊只有一個(gè)主線程,必須等到run()方法里面的代碼執(zhí)行完畢,才會繼續(xù)執(zhí)行下面的代碼,這樣就沒有達(dá)到寫線程的目的。
下面我們通過一個(gè)很經(jīng)典的題目來理解一下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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)該是什么呢?沒錯(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í)間片,開始執(zhí)行,調(diào)用pong()方法打印出”pong”。
如果感興趣,還可以多加幾條語句自己看看效果。
二、源碼
那么他們本質(zhì)上的區(qū)別在哪里,我們來看一下源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
/**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實(shí)現(xiàn)的,看來調(diào)用系統(tǒng)底層還是要通過C語言。這也就是為什么start()方法可以實(shí)現(xiàn)多線程。而調(diào)用run()方法,其實(shí)只是調(diào)用runnable里面自己實(shí)現(xiàn)的run()方法。
我們再看看Thread里run()的源碼:
1
2
3
4
5
6
|
@Override public void run() { if (target != null ) { target.run(); } } |
如果target不為空,則調(diào)用target的run()方法,那么target是什么:
1
2
|
/* What will be run. */ private Runnable target; |
其實(shí)就是一個(gè)Runnable接口,正如上面代碼中new Thread的部分,其實(shí)我們就是在實(shí)現(xiàn)它的run()方法。所以如果直接調(diào)用run,就和一個(gè)普通的方法沒什么區(qū)別,是不會創(chuàng)建新的線程的,因?yàn)閴焊蜎]執(zhí)行start0方法。
三、實(shí)現(xiàn)
前面說了,繼承Thread類和實(shí)現(xiàn)Runnable接口都可以定義一個(gè)線程,那么他們又有什么區(qū)別呢?
在《Java核心技術(shù)卷1 第9版》第627頁提到。可以通過一下代碼構(gòu)建Thread的子類定義一個(gè)線程:
1
2
3
4
5
|
class MyThread extends Thread { public void run() { //do Something } } |
然后,實(shí)例化一個(gè)對象,調(diào)用其start方法。不過這個(gè)方法不推薦。應(yīng)該減少需要并行運(yùn)行的任務(wù)數(shù)量。如果任務(wù)很多,要為每個(gè)任務(wù)創(chuàng)建一個(gè)獨(dú)立的線程所付出的代價(jià)太多,當(dāng)然可以用線程池來解決。
實(shí)現(xiàn)Runnable接口所具有的優(yōu)勢:
1、避免Java單繼承的問題
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()沒有實(shí)現(xiàn)多線程。
以上所述是小編給大家介紹的Java中啟動(dòng)線程start和run方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:http://blog.csdn.net/lai_li/article/details/53070141?locationNum=13&fps=1