Warning: include_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 2

Warning: include_once(http://www.confrage.com/bin/header.php): failed to open stream: no suitable wrapper could be found in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 2

Warning: include_once(): Failed opening 'http://www.confrage.com/bin/header.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 2
スレッドの状態を調べる | Java | 株式会社CONFRAGE
Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 17

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/menu.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 17
スレッドの状態を調べる
スレッドが実行されているかどうか調べるには isAlive() メソッドを使用します。
スレッドが実行中の場合は true を返します。
スレッドが実行中でない場合は false を返します。
以下、スレッドクラスの例を見てください。例では実行中スレッドの名前を出力します。

public class MyThread implements Runnable{
    public void run(){
        for (int i=0;i<10;i++){
            System.out.println(Thread.currentThread().getName());
        }
    }
}
以下のTESTクラスでは、上記スレッドクラスをインスタンス化し、スレッドを実行しています。
そのあとisAlive()メソッドを使用してスレッドが終了するまでwhile文でmainのスレッドを止め、isAlive()メソッドの戻り値がfalseになった時点でループを抜けて標準出力しています。

public class TEST {
    public static void main(String[] args){
        MyThread thread1 = new MyThread();
        
        Thread th = new Thread(thread1);
        th.start();

        while(th.isAlive());
        
        System.out.println("状態: " + th.isAlive());
    }
}
上記、Javaアプリケーションを実行すると、スレッド名が表示され、スレッドが実行終了するとwhile文を抜けてスレッドの状態が表示されます。
出力結果は以下のようになります。

Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
Thread-0
状態: false

Back to top

Information

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 98

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/side_bar.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 98

Warning: include_once(/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php): failed to open stream: No such file or directory in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 107

Warning: include_once(): Failed opening '/home/sites/heteml/users/m/o/r/moriya-h5/web/confrage/bin/seo.php' for inclusion (include_path='.:/usr/local/php/5.6/lib/php') in /home/users/2/moriya-h5/web/confrage/java/thread/isalive/isalive.html on line 107