一、引子
synchronized 會(huì)阻塞線程纵穿,AQS 也會(huì)阻塞線程下隧。那么這兩種情況,阻塞后谓媒,線程的狀態(tài)是什么汪拥,是 waiting 還是 blocked。雖然好像知道篙耗,但不能確定。在網(wǎng)上搜索后宪赶,經(jīng)過指引宗弯,找到 Thread.State 這個(gè)內(nèi)部枚舉類型。
/**
* A thread state. A thread can be in one of the following states:
* <ul>
* <li>{@link #NEW}<br>
* A thread that has not yet started is in this state.
* </li>
* <li>{@link #RUNNABLE}<br>
* A thread executing in the Java virtual machine is in this state.
* </li>
* <li>{@link #BLOCKED}<br>
* A thread that is blocked waiting for a monitor lock
* is in this state.
* </li>
* <li>{@link #WAITING}<br>
* A thread that is waiting indefinitely for another thread to
* perform a particular action is in this state.
* </li>
* <li>{@link #TIMED_WAITING}<br>
* A thread that is waiting for another thread to perform an action
* for up to a specified waiting time is in this state.
* </li>
* <li>{@link #TERMINATED}<br>
* A thread that has exited is in this state.
* </li>
* </ul>
*
* <p>
* A thread can be in only one state at a given point in time.
* These states are virtual machine states which do not reflect
* any operating system thread states.
*
* @since 1.5
* @see #getState
*/
public enum State {
/**
* Thread state for a thread which has not yet started.
*/
NEW,
/**
* Thread state for a runnable thread. A thread in the runnable
* state is executing in the Java virtual machine but it may
* be waiting for other resources from the operating system
* such as processor.
*/
RUNNABLE,
/**
* Thread state for a thread blocked waiting for a monitor lock.
* A thread in the blocked state is waiting for a monitor lock
* to enter a synchronized block/method or
* reenter a synchronized block/method after calling
* {@link Object#wait() Object.wait}.
*/
BLOCKED,
/**
* Thread state for a waiting thread.
* A thread is in the waiting state due to calling one of the
* following methods:
* <ul>
* <li>{@link Object#wait() Object.wait} with no timeout</li>
* <li>{@link #join() Thread.join} with no timeout</li>
* <li>{@link LockSupport#park() LockSupport.park}</li>
* </ul>
*
* <p>A thread in the waiting state is waiting for another thread to
* perform a particular action.
*
* For example, a thread that has called <tt>Object.wait()</tt>
* on an object is waiting for another thread to call
* <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
* that object. A thread that has called <tt>Thread.join()</tt>
* is waiting for a specified thread to terminate.
*/
WAITING,
/**
* Thread state for a waiting thread with a specified waiting time.
* A thread is in the timed waiting state due to calling one of
* the following methods with a specified positive waiting time:
* <ul>
* <li>{@link #sleep Thread.sleep}</li>
* <li>{@link Object#wait(long) Object.wait} with timeout</li>
* <li>{@link #join(long) Thread.join} with timeout</li>
* <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
* <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
* </ul>
*/
TIMED_WAITING,
/**
* Thread state for a terminated thread.
* The thread has completed execution.
*/
TERMINATED;
}
注釋已經(jīng)寫的很清楚了搂妻。
重點(diǎn)來看 WAITING 和 BLOCKED 這兩種狀態(tài)蒙保。
二、BLOCKED
A thread that is blocked waiting for a monitor lock is in this state.
Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked state is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object#wait() Object.wait.
這樣看來欲主,blocked 狀態(tài)僅與 synchronized 關(guān)鍵字引起線程阻塞有關(guān)邓厕。
三逝嚎、WAITING
A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
Thread state for a waiting thread. A thread is in the waiting state due to calling one of the following methods:
- Object#wait() Object.wait with no timeout
- #join() Thread.join with no timeout
- LockSupport#park() LockSupport.park
A thread in the waiting state is waiting for another thread to perform a particular action.
For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate.
我們知道,AQS 內(nèi)部就是依賴 LockSupport.park 阻塞線程详恼,所以在 AQS 中被阻塞的線程處于 waiting 狀態(tài)补君。
四、總結(jié)
blocked 和 waiting 是 Java 線程的兩種阻塞狀態(tài)昧互。
因?yàn)闋幱?synchronized 的 monitor 對(duì)象而發(fā)生阻塞的線程處于 blocked 狀態(tài)挽铁。
而 AQS 中的阻塞線程處于 waiting 狀態(tài)。
兩種狀態(tài)的區(qū)別:
- 阻塞狀態(tài) 涉及到同步鎖的競(jìng)爭敞掘。當(dāng)一個(gè)線程試圖進(jìn)入一個(gè)synchronized區(qū)塊或方法叽掘,但其他線程已經(jīng)持有了相關(guān)的鎖,它就會(huì)進(jìn)入阻塞狀態(tài)玖雁。這里的關(guān)鍵是對(duì)象監(jiān)視器鎖的競(jìng)爭更扁。
- 等待狀態(tài) 則涉及到線程間的通信。一個(gè)線程在等待狀態(tài)赫冬,意味著它正在等待其他線程的信號(hào)或干預(yù)浓镜。例如,通過wait()方法等待其他線程調(diào)用notify()或notifyAll()面殖。