成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

Java線程的狀態(tài)及切換

MkkHou / 2017人閱讀

摘要:如果發(fā)生了動作,則會從等待池當(dāng)中喚醒一個線程重新恢復(fù)到狀態(tài),如果是操作,則喚醒所有等待線程。當(dāng)時間達(dá)到時觸發(fā)線程回到工作狀態(tài)。線程終止?fàn)顟B(tài)線程結(jié)束了,就處于這種狀態(tài),也就是方法運行完了。線程狀態(tài)切換詳細(xì)圖線程狀態(tài)源碼

線程狀態(tài) (1)NEW(新建尚未運行/啟動)

還沒調(diào)用start,或者調(diào)用了start()方法,不一定立即改變線程狀態(tài),中間可能需要一些步驟才完成一個線程的啟動。

(2)RUNNABLE(處于可運行狀態(tài):正在運行或準(zhǔn)備運行)

start調(diào)用結(jié)束,線程由NEW變成RUNNABLE,存活著,并嘗試占用CPU資源,yield操作時,線程還是Runnable狀態(tài),只是它有一個細(xì)節(jié)的內(nèi)部變化,做一個簡單的讓步。在Java層面是Runnable的狀態(tài),并不代表一定處于運行中的狀態(tài),比如BIO中,線程正阻塞在網(wǎng)絡(luò)等待的時候,看到的狀態(tài)依然是Runnable狀態(tài),而底層線程已經(jīng)被阻塞住了。

(3)BLOCKED(等待獲取鎖時進(jìn)入的狀態(tài))

線程被掛起了,原因通常是因為它在等待一個鎖,當(dāng)某個synchronized正好有線程在使用時,一個線程嘗試進(jìn)入這個臨界區(qū),就會被阻塞,直到另一個線程走完臨界區(qū)或發(fā)生了相應(yīng)鎖對象的wait操作后,它才有機會去爭奪進(jìn)入臨界區(qū)的權(quán)利。當(dāng)搶到鎖之后,才會從blocked狀態(tài)恢復(fù)到runnable狀態(tài)。這個狀態(tài)它好像什么也不做一樣。

(4)WAITING(通過wait方法進(jìn)入的等待)

當(dāng)wait,join,park方法調(diào)用時,進(jìn)入waiting狀態(tài)。前提是這個線程已經(jīng)擁有鎖了。

blocked和waiting狀態(tài)的區(qū)別是:
A、blocked是虛擬機認(rèn)為程序還不能進(jìn)入某個區(qū)域,因為同時進(jìn)去就會有問題,這是一塊臨界區(qū)。
B、發(fā)生wait等操作的先決條件是要進(jìn)入臨界區(qū),也就是線程已經(jīng)拿到鎖了,自己可能進(jìn)去做了一些事情,但此時通過判定業(yè)務(wù)上的參數(shù),發(fā)現(xiàn)還有一些其他配合的資源沒有準(zhǔn)備充分,那么自己就等等再做其他事情。

在waiting狀態(tài)下,如果發(fā)生了interrupt操作,則處于該狀態(tài)的線程在內(nèi)部會拋出一個InterruptedException,這個異常應(yīng)當(dāng)在run方法內(nèi)捕獲,使得run方法正常地執(zhí)行完成,當(dāng)然捕獲異常后,是決定讓線程繼續(xù)運行,還是結(jié)束等要根據(jù)業(yè)務(wù)場景才處理。

如果發(fā)生了notify動作,則會從等待池當(dāng)中喚醒一個線程重新恢復(fù)到Runnable狀態(tài),如果是notifyall操作,則喚醒所有等待線程。

(5)TIMED_WAITING(通過sleep或wait timeout方法進(jìn)入的限期等待的狀態(tài))

通過wait(t),sleep(t),join(t),parkNanos,parkUntil等方法進(jìn)入此狀態(tài)。當(dāng)時間達(dá)到時觸發(fā)線程回到工作狀態(tài)Runnable。

interrupt只對處于waiting或timed_waiting狀態(tài)的線程起作用,對其他狀態(tài)不起作用。

(6)TERMINATED(線程終止?fàn)顟B(tài))

線程結(jié)束了,就處于這種狀態(tài),也就是run方法運行完了。這只是Java語言級別的一種狀態(tài),在操作系統(tǒng)內(nèi)部可能已經(jīng)注銷了相應(yīng)的線程,或者將它復(fù)用給其他需要使用線程的請求。

線程狀態(tài)切換

詳細(xì)圖

線程狀態(tài)源碼
/**
     * A thread state.  A thread can be in one of the following states:
     * 
    *
  • {@link #NEW}
    * A thread that has not yet started is in this state. *
  • *
  • {@link #RUNNABLE}
    * A thread executing in the Java virtual machine is in this state. *
  • *
  • {@link #BLOCKED}
    * A thread that is blocked waiting for a monitor lock * is in this state. *
  • *
  • {@link #WAITING}
    * A thread that is waiting indefinitely for another thread to * perform a particular action is in this state. *
  • *
  • {@link #TIMED_WAITING}
    * A thread that is waiting for another thread to perform an action * for up to a specified waiting time is in this state. *
  • *
  • {@link #TERMINATED}
    * A thread that has exited is in this state. *
  • *
* *

* 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: *

    *
  • {@link Object#wait() Object.wait} with no timeout
  • *
  • {@link #join() Thread.join} with no timeout
  • *
  • {@link 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. */ 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: *

    *
  • {@link #sleep Thread.sleep}
  • *
  • {@link Object#wait(long) Object.wait} with timeout
  • *
  • {@link #join(long) Thread.join} with timeout
  • *
  • {@link LockSupport#parkNanos LockSupport.parkNanos}
  • *
  • {@link LockSupport#parkUntil LockSupport.parkUntil}
  • *
*/ TIMED_WAITING, /** * Thread state for a terminated thread. * The thread has completed execution. */ TERMINATED; }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://m.hztianpu.com/yun/65814.html

相關(guān)文章

  • (二)線程應(yīng)用挑戰(zhàn)

    摘要:上下文切換會影響到線程的執(zhí)行速度,對于系統(tǒng)來說意味著會消耗大量的時間減少上下文切換的方式無鎖并發(fā)編程,在多線程競爭鎖時,會導(dǎo)致大量的上下文切換。線程在中的使用在中實現(xiàn)多線程的方式比較簡單,因為中提供了非常方便的來實現(xiàn)多線程。 文章簡介 上一篇文章我們了解了進(jìn)程和線程的發(fā)展歷史、線程的生命周期、線程的優(yōu)勢和使用場景,這一篇,我們從Java層面更進(jìn)一步了解線程的使用 內(nèi)容導(dǎo)航 并發(fā)編程的...

    hqman 評論0 收藏0
  • Java 應(yīng)用性能優(yōu)化(1)

    摘要:應(yīng)用性能優(yōu)化是一個程序員必須要考慮的問題,典型的性能問題如頁面響應(yīng)慢接口超時,服務(wù)器負(fù)載高并發(fā)數(shù)低,數(shù)據(jù)庫頻繁死鎖等。診斷對于主要關(guān)注平均負(fù)載,使用率,上下文切換次數(shù)。應(yīng)用診斷及工具應(yīng)用代碼性能問題是相對好解決的一類性能問題。 Java 應(yīng)用性能優(yōu)化是一個程序員必須要考慮的問題,典型的性能問題如頁面響應(yīng)慢、接口超時,服務(wù)器負(fù)載高、并發(fā)數(shù)低,數(shù)據(jù)庫頻繁死鎖等。Java應(yīng)用性能的瓶頸點非常...

    tulayang 評論0 收藏0
  • Java線程基礎(chǔ)知識(一)

    摘要:多線程一線程模型實現(xiàn)線程有三種方式使用內(nèi)核線程實現(xiàn)使用用戶線程實現(xiàn)和使用用戶線程加輕量級進(jìn)程混合實現(xiàn)。這種輕量級進(jìn)程與內(nèi)核線程之間的關(guān)系稱為一對一的線程模型。是通知所有等待對象控制權(quán)的線程繼續(xù)運行。 Java多線程 一、Java線程模型 實現(xiàn)線程有三種方式:使用內(nèi)核線程實現(xiàn)、使用用戶線程實現(xiàn)和使用用戶線程加輕量級進(jìn)程混合實現(xiàn)。內(nèi)核線程是直接由操作系統(tǒng)內(nèi)核支持的線程,通過內(nèi)核完成線程切換...

    馬龍駒 評論0 收藏0
  • Java視角理解系統(tǒng)結(jié)構(gòu) (一) CPU上下文切換

    摘要:本文是從視角理解系統(tǒng)結(jié)構(gòu)連載文章在高性能編程時經(jīng)常接觸到多線程起初我們的理解是多個線程并行地執(zhí)行總比單個線程要快就像多個人一起干活總比一個人干要快然而實際情況是多線程之間需要競爭設(shè)備或者競爭鎖資源,導(dǎo)致往往執(zhí)行速度還不如單個線程在這里有一個 本文是從Java視角理解系統(tǒng)結(jié)構(gòu)連載文章 在高性能編程時,經(jīng)常接觸到多線程. 起初我們的理解是, 多個線程并行地執(zhí)行總比單個線程要快, 就像多個...

    yuxue 評論0 收藏0
  • Java面試 32個核心必考點完全解析

    摘要:如問到是否使用某框架,實際是是問該框架的使用場景,有什么特點,和同類可框架對比一系列的問題。這兩個方向的區(qū)分點在于工作方向的側(cè)重點不同。 [TOC] 這是一份來自嗶哩嗶哩的Java面試Java面試 32個核心必考點完全解析(完) 課程預(yù)習(xí) 1.1 課程內(nèi)容分為三個模塊 基礎(chǔ)模塊: 技術(shù)崗位與面試 計算機基礎(chǔ) JVM原理 多線程 設(shè)計模式 數(shù)據(jù)結(jié)構(gòu)與算法 應(yīng)用模塊: 常用工具集 ...

    JiaXinYi 評論0 收藏0

發(fā)表評論

0條評論

閱讀需要支付1元查看
<