Java 例 - スレッド状態の取得

Java 例

Java スレッドのライフサイクルにおいて、Thread クラスには列挙型 State があり、スレッドのいくつかの状態を定義しています。それぞれ次のとおりです:

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated

各状態の説明:

1. 初期状態 - NEW

説明:

public static final Thread.State NEW

Runnable インターフェースを実装し、Thread を継承すると、スレッドクラスを得ることができます。new でインスタンスを生成すると、スレッドは初期状態に入ります。

2. RUNNABLE

説明:

public static final Thread.State RUNNABLE

2.1. 実行可能状態

実行可能状態とは、単に実行する資格があることを意味します。スケジューラがあなたを選択しなければ、永遠に実行可能状態のままです。

スレッドの start() メソッドを呼び出すと、このスレッドは実行可能状態に入ります。

現在のスレッドの sleep() メソッドが終了した場合、他のスレッドの join() が終了した場合、ユーザーの入力を待ち終えた場合、あるスレッドがオブジェクトロックを取得した場合、これらのスレッドも実行可能状態に入ります。

現在のスレッドのタイムスライスが使い切られた場合、または現在のスレッドの yield() メソッドが呼び出された場合、現在のスレッドは実行可能状態に入ります。

ロックプール内のスレッドは、オブジェクトロックを取得した後、実行可能状態に入ります。

2.2. 実行中状態

スレッドスケジューラが実行可能プールから1つのスレッドを現在のスレッドとして選択したときの状態です。これがスレッドが実行状態に入る唯一の方法でもあります。

3. ブロック状態 - BLOCKED

説明:

public static final Thread.State BLOCKED

ブロック状態は、スレッドが synchronized キーワードで修飾されたメソッドまたはコードブロック(ロックの取得)に入るときにブロックされている状態です。

4. 待機 - WAITING

説明:

public static final Thread.State WAITING

この状態にあるスレッドには CPU 実行時間が割り当てられず、明示的に起こされるのを待つ必要があります。そうしないと、無期限に待機し続ける状態になります。

5. タイムアウト待機 - TIMED_WAITING

説明:

public static final Thread.State TIMED_WAITING

この状態にあるスレッドには CPU 実行時間が割り当てられませんが、他のスレッドに明示的に起こされるのを無期限に待つ必要はなく、一定時間に達すると自動的に起きます。

6. 終了状態 - TERMINATED

説明:

public static final Thread.State TERMINATED

スレッドの run() メソッドが完了したとき、またはメインスレッドの main() メソッドが完了したとき、そのスレッドは終了したと見なされます。このスレッドオブジェクトは生きているかもしれませんが、単独で実行されるスレッドではなくなっています。スレッドはいったん終了すると、再び生き返ることはできません。

終了したスレッドで start() メソッドを呼び出すと、java.lang.IllegalThreadStateException 例外がスローされます。

次の例は、スレッドの状態を取得する方法を示しています:

Main.java ファイル

//Java プログラム - スレッド状態のデモ class thread implements Runnable { public void run() { //thread2 - タイムアウト待機 try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("State of thread1 while it called join() method on thread2 -"+ Test.thread1.getState()); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } public class Test implements Runnable { public static Thread thread1; public static Test obj; public static void main(String[] args) { obj = new Test(); thread1 = new Thread(obj); //thread1 を作成し、現在は初期状態です System.out.println("State of thread1 after creating it - " + thread1.getState()); thread1.start(); //thread1 - 実行可能状態 System.out.println("State of thread1 after calling .start() method on it - " + thread1.getState()); } public void run() { thread myThread = new thread(); Thread thread2 = new Thread(myThread); //thread1 を作成し、現在は初期状態です System.out.println("State of thread2 after creating it - "+ thread2.getState()); thread2.start(); //thread2 - 実行可能状態 System.out.println("State of thread2 after calling .start() method on it - " + thread2.getState()); // moving thread1 to timed waiting state try { //moving - タイムアウト待機 Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("State of thread2 after calling .sleep() method on it - "+ thread2.getState() ); try { //thread2 の終了を待機 thread2.join(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("State of thread2 when it has finished it's execution - " + thread2.getState()); } }

上記のコードを実行した出力結果は次のとおりです:

State of thread1 after creating it - NEW
State of thread1 after calling .start() method on it - RUNNABLE
State of thread2 after creating it - NEW
State of thread2 after calling .start() method on it - RUNNABLE
State of thread2 after calling .sleep() method on it - TIMED_WAITING
State of thread1 while it called join() method on thread2 -WAITING
State of thread2 when it has finished it's execution - TERMINATED

Java 例

その他の拡張