之前有簡(jiǎn)單介紹過(guò)java多線程的使用,已經(jīng)Thread類(lèi)和Runnable類(lèi),為了更好地理解多線程,本文就Thread進(jìn)行詳細(xì)的分析。
start()
我們先來(lái)看看API中對(duì)于該方法的介紹:
使該線程開(kāi)始執(zhí)行;Java 虛擬機(jī)調(diào)用該線程的 run 方法。
結(jié)果是兩個(gè)線程并發(fā)地運(yùn)行;當(dāng)前線程(從調(diào)用返回給 start 方法)和另一個(gè)線程(執(zhí)行其 run 方法)。
多次啟動(dòng)一個(gè)線程是非法的。特別是當(dāng)線程已經(jīng)結(jié)束執(zhí)行后,不能再重新啟動(dòng)。
用start方法來(lái)啟動(dòng)線程,真正實(shí)現(xiàn)了多線程運(yùn)行,這時(shí)無(wú)需等待run方法體代碼執(zhí)行完畢而直接繼續(xù)執(zhí)行下面的代碼。通過(guò)調(diào)用Thread類(lèi)的 start()方法來(lái)啟動(dòng)一個(gè)線程,這時(shí)此線程處于就緒(可運(yùn)行)狀態(tài),并沒(méi)有運(yùn)行,一旦得到cpu時(shí)間片,就開(kāi)始執(zhí)行run()方法,這里方法 run()稱(chēng)為線程體,它包含了要執(zhí)行的這個(gè)線程的內(nèi)容,Run方法運(yùn)行結(jié)束,此線程隨即終止。
start方法是開(kāi)啟線程的方法,使用后java會(huì)創(chuàng)建一個(gè)新的線程執(zhí)行run里的方法。這是一個(gè)小demo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
for ( int i= 0 ;i< 3 ;i++){ Thread t= new Thread( new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+ " start" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ " end" ); } }); t.start(); } System.out.println( "it is over" ); |
執(zhí)行結(jié)果:
it is over
Thread-1 start
Thread-0 start
Thread-2 start
Thread-0 end
Thread-1 end
Thread-2 end
由于多線程是有隨機(jī)性的,所以每次的結(jié)果可能都不一樣,這一點(diǎn)也是我們需要注意的,線程的執(zhí)行順序和調(diào)用順序并不一致。
run()
run方法就是調(diào)用Thread設(shè)置的Runnable的run方法,將上面的demo進(jìn)行修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
for ( int i= 0 ;i< 3 ;i++){ Thread t= new Thread( new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+ " start" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ " end" ); } }); t.run(); } System.out.println( "it is over" ); |
執(zhí)行結(jié)果:
main start
main end
main start
main end
main start
main end
it is over
run方法的直接結(jié)果和start有很大的差別,完全是按順序執(zhí)行,并沒(méi)有開(kāi)啟新線程。
stop()
stop方法是強(qiáng)制停止線程的執(zhí)行,是非安全的,不要使用此方法。在調(diào)用stop時(shí), 會(huì)對(duì)鎖定的資源進(jìn)行釋放,但這種釋放是非一致的,容易引起程序問(wèn)題。如果想要控制線程的停止,可以使用自定義變量來(lái)判斷或者isInterrupted()方法:
1
2
3
4
5
6
7
8
9
|
class Thread1 extends Thread { @Override public void run() { //判斷線程體是否運(yùn)行 while (!isInterrupted()) { // Do Something } } } |
interrupt()
interrupt的作用是通知線程,你已經(jīng)被中斷的,但具體的中斷執(zhí)行需要在線程自定義處理,甚至你可以不理會(huì)繼續(xù)執(zhí)行。具體的中孤單是會(huì)線程執(zhí)行join、wait、sleep方法時(shí),拋出InterruptedException。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
Thread t1 = new Thread( new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+ " start" ); try { for ( int i= 0 ;i< 100000 ;i++){ System.out.println(i+ "" ); Thread.sleep( 1 ); } } catch (InterruptedException e) { System.out.println( "the thread is interrupted" ); //可以在這里做資源釋放,日志記錄等 e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ " end" ); } }); t1.start(); Thread.sleep( 100 ); t1.interrupt(); |
執(zhí)行結(jié)果:
1
2
3
4
5
6
7
8
9
10
|
65 66 67 68 the thread is interrupted java.lang.InterruptedException: sleep interrupted Thread- 0 end at java.lang.Thread.sleep(Native Method) at com.wk.aqi.act.Test$ 1 .run(Test.java: 23 ) at java.lang.Thread.run(Thread.java: 745 ) |
isInterrupted()
判斷線程是否中斷,在執(zhí)行上面的interrupt方法后,會(huì)return true。
setPriority(int newPriority)和getPriority()
設(shè)置線程的優(yōu)先級(jí)和獲取線程的優(yōu)先級(jí),cpu分配的資源給側(cè)重給priority高的線程。
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
|
Thread t1 = new Thread( new Runnable() { @Override public void run() { long t = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName()+ " start" ); for ( int i= 0 ;i< 1000 ;i++){ try { Thread.sleep( 1 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+ " t1 end " +(System.currentTimeMillis()-t)); } }); Thread t2 = new Thread( new Runnable() { @Override public void run() { long t = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName()+ " start" ); for ( int i= 0 ;i< 1000 ;i++){ try { Thread.sleep( 1 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName()+ " t2 end " +(System.currentTimeMillis()-t)); } }); t1.setPriority( 10 ); t2.setPriority( 1 ); t2.start(); t1.start(); |
執(zhí)行結(jié)果:
1
2
3
4
|
Thread- 0 start Thread- 1 start Thread- 0 t1 end 1357 Thread- 1 t2 end 1371 |
在優(yōu)先級(jí)一樣的情況下,t1和t2是幾乎同時(shí)完成的,在優(yōu)先級(jí)不一樣的情況,有明顯的差別。
getName()
比較簡(jiǎn)單,獲取線程的名稱(chēng)。
join()和join(long millis)
jion方法的作用是等待線程執(zhí)行完成,join(long millis)可以設(shè)置最長(zhǎng)等待時(shí)間。比如主線程需要等待子線程完成,獲取子線程的結(jié)果后才能繼續(xù)往下執(zhí)行,這時(shí)候就可以使用join方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
Thread t1 = new Thread( new Runnable() { @Override public void run() { long t = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName()+ " start" ); try { Thread.sleep( 1000 ); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(Thread.currentThread().getName()+ " t1 end " +(System.currentTimeMillis()-t)); } }); t1.start(); t1.join(); System.out.println( "等待t1執(zhí)行完,再執(zhí)行" ); |
執(zhí)行結(jié)果:
1
2
3
|
Thread- 0 start Thread- 0 t1 end 1001 等待t1執(zhí)行完,再執(zhí)行 |
總結(jié)
以上就是本文關(guān)于java多線程Thread的實(shí)現(xiàn)方法代碼詳解的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專(zhuān)題,如有不足之處,歡迎留言指出。
原文鏈接:https://segmentfault.com/a/1190000012213171