Java 線程狀態(tài)之 blocked 和 waiting 的區(qū)別

一、引子

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()面殖。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末竖哩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子脊僚,更是在濱河造成了極大的恐慌相叁,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件辽幌,死亡現(xiàn)場(chǎng)離奇詭異增淹,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)乌企,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門虑润,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人加酵,你說我怎么就攤上這事拳喻。” “怎么了猪腕?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵冗澈,是天一觀的道長。 經(jīng)常有香客問我陋葡,道長亚亲,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮捌归,結(jié)果婚禮上肛响,老公的妹妹穿的比我還像新娘。我一直安慰自己惜索,他們只是感情好特笋,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著门扇,像睡著了一般雹有。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上臼寄,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天霸奕,我揣著相機(jī)與錄音,去河邊找鬼吉拳。 笑死质帅,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的留攒。 我是一名探鬼主播煤惩,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼炼邀!你這毒婦竟也來了魄揉?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤拭宁,失蹤者是張志新(化名)和其女友劉穎洛退,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體杰标,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡兵怯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了腔剂。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片媒区。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖掸犬,靈堂內(nèi)的尸體忽然破棺而出袜漩,到底是詐尸還是另有隱情,我是刑警寧澤湾碎,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布宙攻,位于F島的核電站,受9級(jí)特大地震影響胜茧,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一呻顽、第九天 我趴在偏房一處隱蔽的房頂上張望雹顺。 院中可真熱鬧,春花似錦廊遍、人聲如沸嬉愧。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽没酣。三九已至,卻和暖如春卵迂,著一層夾襖步出監(jiān)牢的瞬間裕便,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國打工见咒, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留偿衰,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓改览,卻偏偏與公主長得像下翎,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子宝当,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355