Javaスレッドのライフサイクルプレゼンテーションスレッドのステータス付コード実装

3121 ワード

プレゼンテーションスレッドのライフサイクル 
Javaプログラミングの考え方は以下の通りです.
  • スレッドステータス:new、Runnable、Blocked、Dead
  • 新規(new):スレッドが作成されると、この状態は短くなるだけです.この時点で、必要なシステムリソースが割り当てられ、初期化が実行されます.スレッドはCPU時間を取得する資格があり、スケジューラはこのスレッドを実行可能状態またはブロック状態に変更します.(isAlive()メソッドが呼び出されるとfalseが返されます)
  • 準備完了(Runnable):この状態では、スケジューラがスレッドにタイムスライスを割り当てる限り、スレッドは実行されます.すなわち,任意の時点でスレッドを実行してもよいし,実行しなくてもよい.スケジューラがスレッドにタイムスライスを割り当てることができる限り、実行できます.これは死と閉塞状態とは異なる.
  • ブロック(Blocked):スレッドは実行できますが、実行を阻止する条件があります.スレッドがブロックされている場合、スケジューラはスレッドを無視し、スレッドにCPU時間を割り当てることはありません.スレッドが再び準備完了状態になるまで、操作を実行することはできません.
  • デッド(Dead):デッドまたはエンドのスレッドはスケジューリング可能ではなく、CPU時間が得られなくなり、タスクが終了したり、実行可能ではなくなったりします.タスクの死亡の通常の方法はrun()メソッドから返されるが、タスクのスレッドは中断されることもある.


  • jdkドキュメントには、次のように記述されています.
  • A thread can be in one of the following states:
  • NEW A thread that has not yet started is in this state.
  • RUNNABLE A thread executing in the Java virtual machine is in this state.
  • BLOCKED A thread that is blocked waiting for a monitor lock is in this state.
  • WAITING A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
  • 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.
  • 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.
    このうちBLOCKEDとWAITINGの状態は少し疑問で、stackoverflowで以下の説明を見つけて理解を助けます.
    1. The difference is relatively simple.
    In the  BLOCKED  state, a thread is about to enter a  synchronized  block, but there is another thread currently running inside a  synchronized  block on the same object. The first thread must then wait for the second thread to exit its block.
    In the  WAITING  state, a thread is waiting for a signal from another thread. This happens typically by calling  Object.wait() , or  Thread.join() . The thread will then remain in this state until another thread calls  Object.notify() , or dies.
    2. A thread goes to wait state once it calls  wait()  on an Object. This is called Waiting State. Once a thread reaches waiting state, it will need to wait till some other thread  notify()  or  notifyAll()  on the object.
    Once this thread is notified, it will not be runnable. It might be that other threads are also notified(using  notifyAll() ) or the first thread has not finished his work, so it is still blocked till it gets its chance. This is called Blocked State.
    Once other threads have left and its this thread chance, it moves to Runnable state after that it is eligible pick up work based on JVM threading mechanism and moves to run state.
    独自のコード実装を添付:プレゼンテーションスレッドのライフサイクル 討論を歓迎します~