線程的狀態(tài)
線程狀態(tài)圖:
說(shuō)明:
線程共包括以下5種狀態(tài)。
1. 新建狀態(tài)(New) : 線程對(duì)象被創(chuàng)建后,就進(jìn)入了新建狀態(tài)。例如,Thread thread = new Thread()。
2. 就緒狀態(tài)(Runnable): 也被稱為“可執(zhí)行狀態(tài)”。線程對(duì)象被創(chuàng)建后,其它線程調(diào)用了該對(duì)象的start()方法,從而來(lái)啟動(dòng)該線程。例如,thread.start()。處于就緒狀態(tài)的線程,隨時(shí)可能被CPU調(diào)度執(zhí)行。
3. 運(yùn)行狀態(tài)(Running) : 線程獲取CPU權(quán)限進(jìn)行執(zhí)行。需要注意的是,線程只能從就緒狀態(tài)進(jìn)入到運(yùn)行狀態(tài)。
4. 阻塞狀態(tài)(Blocked) : 阻塞狀態(tài)是線程因?yàn)槟撤N原因放棄CPU使用權(quán),暫時(shí)停止運(yùn)行。直到線程進(jìn)入就緒狀態(tài),才有機(jī)會(huì)轉(zhuǎn)到運(yùn)行狀態(tài)。阻塞的情況分三種:
(1) 等待阻塞 -- 通過(guò)調(diào)用線程的wait()方法,讓線程等待某工作的完成。
(2) 同步阻塞 -- 線程在獲取synchronized同步鎖失敗(因?yàn)殒i被其它線程所占用),它會(huì)進(jìn)入同步阻塞狀態(tài)。
(3) 其他阻塞 -- 通過(guò)調(diào)用線程的sleep()或join()或發(fā)出了I/O請(qǐng)求時(shí),線程會(huì)進(jìn)入到阻塞狀態(tài)。當(dāng)sleep()狀態(tài)超時(shí)、join()等待線程終止或者超時(shí)、或者I/O處理完畢時(shí),線程重新轉(zhuǎn)入就緒狀態(tài)。
5. 死亡狀態(tài)(Dead) : 線程執(zhí)行完了或者因異常退出了run()方法,該線程結(jié)束生命周期。
這5種狀態(tài)涉及到的內(nèi)容包括Object類, Thread和synchronized關(guān)鍵字。這些內(nèi)容我們會(huì)在后面的章節(jié)中逐個(gè)進(jìn)行學(xué)習(xí)。
Object類,定義了wait(), notify(), notifyAll()等休眠/喚醒函數(shù)。
Thread類,定義了一些列的線程操作函數(shù)。例如,sleep()休眠函數(shù), interrupt()中斷函數(shù), getName()獲取線程名稱等。
synchronized,是關(guān)鍵字;它區(qū)分為synchronized代碼塊和synchronized方法。synchronized的作用是讓線程獲取對(duì)象的同步鎖。
在后面詳細(xì)介紹wait(),notify()等方法時(shí),我們會(huì)分析為什么“wait(), notify()等方法要定義在Object類,而不是Thread類中”。
實(shí)現(xiàn)多線程的兩種方式:Thread和Runnable
Runnable 是一個(gè)接口,該接口中只包含了一個(gè)run()方法。它的定義如下:
1
2
3
|
public interface Runnable { public abstract void run(); } |
Runnable的作用,實(shí)現(xiàn)多線程。我們可以定義一個(gè)類A實(shí)現(xiàn)Runnable接口;然后,通過(guò)new Thread(new A())等方式新建線程。
Thread 是一個(gè)類。Thread本身就實(shí)現(xiàn)了Runnable接口。它的聲明如下:
public class Thread implements Runnable {}
Thread的作用,實(shí)現(xiàn)多線程。
Thread和Runnable的異同點(diǎn):
Thread 和 Runnable 的相同點(diǎn):都是“多線程的實(shí)現(xiàn)方式”。
Thread 和 Runnable 的不同點(diǎn):
Thread 是類,而Runnable是接口;Thread本身是實(shí)現(xiàn)了Runnable接口的類。我們知道“一個(gè)類只能有一個(gè)父類,但是卻能實(shí)現(xiàn)多個(gè)接口”,因此Runnable具有更好的擴(kuò)展性。
此外,Runnable還可以用于“資源的共享”。即,多個(gè)線程都是基于某一個(gè)Runnable對(duì)象建立的,它們會(huì)共享Runnable對(duì)象上的資源。
通常,建議通過(guò)“Runnable”實(shí)現(xiàn)多線程!
Thread和Runnable的多線程示例
1.Thread的多線程示例
下面通過(guò)示例更好的理解Thread和Runnable,借鑒網(wǎng)上一個(gè)例子比較具有說(shuō)服性的例子。// ThreadTest.java 源碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
class MyThread extends Thread{ private int ticket= 10 ; public void run(){ for ( int i= 0 ;i< 20 ;i++){ if ( this .ticket> 0 ){ System.out.println( this .getName()+ " 賣票:ticket" + this .ticket--); } } } }; public class ThreadTest { public static void main(String[] args) { // 啟動(dòng)3個(gè)線程t1,t2,t3;每個(gè)線程各賣10張票! MyThread t1= new MyThread(); MyThread t2= new MyThread(); MyThread t3= new MyThread(); t1.start(); t2.start(); t3.start(); } } |
運(yùn)行結(jié)果:
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
|
Thread-0 賣票:ticket10 Thread-1 賣票:ticket10 Thread-2 賣票:ticket10 Thread-1 賣票:ticket9 Thread-0 賣票:ticket9 Thread-1 賣票:ticket8 Thread-2 賣票:ticket9 Thread-1 賣票:ticket7 Thread-0 賣票:ticket8 Thread-1 賣票:ticket6 Thread-2 賣票:ticket8 Thread-1 賣票:ticket5 Thread-0 賣票:ticket7 Thread-1 賣票:ticket4 Thread-2 賣票:ticket7 Thread-1 賣票:ticket3 Thread-0 賣票:ticket6 Thread-1 賣票:ticket2 Thread-2 賣票:ticket6 Thread-2 賣票:ticket5 Thread-2 賣票:ticket4 Thread-1 賣票:ticket1 Thread-0 賣票:ticket5 Thread-2 賣票:ticket3 Thread-0 賣票:ticket4 Thread-2 賣票:ticket2 Thread-0 賣票:ticket3 Thread-2 賣票:ticket1 Thread-0 賣票:ticket2 Thread-0 賣票:ticket1 |
結(jié)果說(shuō)明:
(1) MyThread繼承于Thread,它是自定義個(gè)線程。每個(gè)MyThread都會(huì)賣出10張票。
(2) 主線程main創(chuàng)建并啟動(dòng)3個(gè)MyThread子線程。每個(gè)子線程都各自賣出了10張票。
2.Runnable的多線程示例
下面,我們對(duì)上面的程序進(jìn)行修改。通過(guò)Runnable實(shí)現(xiàn)一個(gè)接口,從而實(shí)現(xiàn)多線程。
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
|
// RunnableTest.java 源碼 class MyThread implements Runnable{ private int ticket= 10 ; public void run(){ for ( int i= 0 ;i< 20 ;i++){ if ( this .ticket> 0 ){ System.out.println(Thread.currentThread().getName()+ " 賣票:ticket" + this .ticket--); } } } }; public class RunnableTest { public static void main(String[] args) { MyThread mt= new MyThread(); // 啟動(dòng)3個(gè)線程t1,t2,t3(它們共用一個(gè)Runnable對(duì)象),這3個(gè)線程一共賣10張票! Thread t1= new Thread(mt); Thread t2= new Thread(mt); Thread t3= new Thread(mt); t1.start(); t2.start(); t3.start(); } } |
運(yùn)行結(jié)果:
1
2
3
4
5
6
7
8
9
10
|
Thread-0 賣票:ticket10 Thread-2 賣票:ticket8 Thread-1 賣票:ticket9 Thread-2 賣票:ticket6 Thread-0 賣票:ticket7 Thread-2 賣票:ticket4 Thread-1 賣票:ticket5 Thread-2 賣票:ticket2 Thread-0 賣票:ticket3 Thread-1 賣票:ticket1 |
結(jié)果說(shuō)明:
(1) 和上面“MyThread繼承于Thread”不同;這里的MyThread實(shí)現(xiàn)了Thread接口。
(2) 主線程main創(chuàng)建并啟動(dòng)3個(gè)子線程,而且這3個(gè)子線程都是基于“mt這個(gè)Runnable對(duì)象”而創(chuàng)建的。運(yùn)行結(jié)果是這3個(gè)子線程一共賣出了10張票。這說(shuō)明它們是共享了MyThread接口的。