天天看點

Java線程狀态轉換

前言:對于Java線程狀态方面的知識點,筆者總感覺朦朦胧胧,趁着最近整理資料,将Java線程狀态方面的知識點總結歸納,以便加深記憶。

1.Java線程狀态值

在Thread類源碼中通過枚舉為線程定義了6種狀态值。

1 public enum State {
 2         /**
 3          * Thread state for a thread which has not yet started.
 4          */
 5         NEW,
 6 
 7         /**
 8          * Thread state for a runnable thread.  A thread in the runnable
 9          * state is executing in the Java virtual machine but it may
10          * be waiting for other resources from the operating system
11          * such as processor.
12          */
13         RUNNABLE,
14 
15         /**
16          * Thread state for a thread blocked waiting for a monitor lock.
17          * A thread in the blocked state is waiting for a monitor lock
18          * to enter a synchronized block/method or
19          * reenter a synchronized block/method after calling
20          * {@link Object#wait() Object.wait}.
21          */
22         BLOCKED,
23 
24         /**
25          * Thread state for a waiting thread.
26          * A thread is in the waiting state due to calling one of the
27          * following methods:
28          * <ul>
29          *   <li>{@link Object#wait() Object.wait} with no timeout</li>
30          *   <li>{@link #join() Thread.join} with no timeout</li>
31          *   <li>{@link LockSupport#park() LockSupport.park}</li>
32          * </ul>
33          *
34          * <p>A thread in the waiting state is waiting for another thread to
35          * perform a particular action.
36          *
37          * For example, a thread that has called <tt>Object.wait()</tt>
38          * on an object is waiting for another thread to call
39          * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
40          * that object. A thread that has called <tt>Thread.join()</tt>
41          * is waiting for a specified thread to terminate.
42          */
43         WAITING,
44 
45         /**
46          * Thread state for a waiting thread with a specified waiting time.
47          * A thread is in the timed waiting state due to calling one of
48          * the following methods with a specified positive waiting time:
49          * <ul>
50          *   <li>{@link #sleep Thread.sleep}</li>
51          *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
52          *   <li>{@link #join(long) Thread.join} with timeout</li>
53          *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
54          *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
55          * </ul>
56          */
57         TIMED_WAITING,
58 
59         /**
60          * Thread state for a terminated thread.
61          * The thread has completed execution.
62          */
63         TERMINATED;
64     }      
NEW          ---- 建立了線程對象但尚未調用start()方法時的狀态。
RUNNABLE     ---- 線程對象調用start()方法後,線程處于可運作狀态,此時線程等待擷取CPU執行權。
BLOCKED      ---- 線程等待擷取鎖時的狀态。
WAITING      ---- 線程處于等待狀态,處于該狀态辨別目前線程需要等待其他線程做出一些特定的操作喚醒自己。
TIME_WAITING ---- 逾時等待狀态,與WAITING不同,在等待指定的時間後會自行傳回。
TERMINATED   ---- 終止狀态,表示目前線程已執行完畢。      

2.線程狀态轉換

  • 看圖了解,下圖對線程狀态轉換描述得非常清楚。
Java線程狀态轉換
  • 繼續看圖,将上下兩張圖檔結合起來,了解就非常容易了。
Java線程狀态轉換
  • 隻要充分了解上述兩張圖檔,筆者相信就可以搞懂Java線程狀态的轉換關系了,如果你還有點懵懂,那麼請看下圖:
Java線程狀态轉換

注:以上三張圖檔均來自于參考文章,對于最後一張圖檔有個小問題:t2.join()方法會釋放鎖。

總結

通過以上三張圖檔的描述,對Java線程狀态轉換的核心要點總結如下:

其實通過上面三張圖檔已經能很好的說明問題了,過多的總結反倒顯得蒼白無力。主要注意WAITING和BLOCKED狀态:這兩個狀态下的線程都未運作,BLOCKED狀态下,線程正等待鎖;WAITING狀态下,線程正等待被喚醒,喚醒後可能進入BLOCKED狀态,繼續等待鎖,或者進入RUNNABLE狀态,重新擷取CPU時間片,繼而等待擷取鎖。

參考:

https://my.oschina.net/mingdongcheng/blog/139263

https://github.com/c-rainstorm/blog/blob/master/java/談談Java線程狀态轉換.md

by Shawn Chen,2019.02.17,下午。

=========================================================

比你優秀的人比你還努力,你有什麼資格不去奮鬥!

__一個有理想的程式員。

繼續閱讀