Java線程狀態(Thread.State)


java.lang.Thread.State

成員內部類

NEW:還未啟動

  Thread state for a thread which has not yet started.

RUNNABLE:正在jvm中運行,但是可能正在等待操作系統的其他資源

  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.

BLOCKED:受阻塞,並且正在等待監視器鎖

  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}.

WAITING:處於等待狀態的線程,正在等待另一個線程執行特定的操作

  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.

  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.

TIMED_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}

TERMINATED

  Thread state for a terminated thread.

  The thread has completed execution.

  

Thread start()

     sleep() sleep(?)

        join()

Object wait() notify() notifyAll()

   wait(?)



 

 

新建(new)

可運行(Runnable)

阻塞(block)

無限期等待(waiting)

限期等待(TIMED_WAITING)

結束(terminated)

 

新創建一個線程,線程處於新建狀態(new)

  使用重載的new Thread()創建一個新的線程

 

調用線程的start()方法,線程進入可運行狀態(Runnable)

  Java線程中的Runnable狀態包括操作系統線程的running和ready狀態,處於就緒狀態的線程一旦獲得CPU使用權,進入運行狀態

  即處於Runnable狀態的線程可能正在運行,也可能正在等待CPU分配時間片

  調用Thread類的start()方法之后  

 

一個線程處於等待(waiting)狀態,表示該線程正在等待其它線程執行特定的操作

  例如,一個線程調用了Object類的空參的wait方法,表示該線程等待其它線程調用該對象的notify或者notifyAll方法

        一個線程調用了Thread類的空參的join方法,表示該線程在等待一個特定的線程終止

  何時進入waiting狀態:

    調用Object類空參的wait()

    調用Thread類空參的join()

 

一個線程在執行過程中,想獲得某一資源的鎖,但是該資源又被其它線程占用,此時,當前線程處於阻塞狀態(例如,進入一個同步方法或者同步代碼塊,要先獲得鎖)

  阻塞和等待的不同是,阻塞狀態是在等待一個排它鎖

              等待狀態實在等待一個事件的發生,如等待時間到,喚醒方法被調用

 

一個線程處於超時等待狀態(timed_waiting),和waiting不同的是,將在特定的時間內自行返回

  何時進入timed_waiting狀態:

    調用Object類有參的wait()

    調用Thread類有參的join()

    調用Thread類的sleep方法(sleep方法沒有空參的重載形式)

 

一個線程執行完畢,進入終止狀態(terminated)

  線程執行完畢

  線程在執行過程中拋出異常

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM