测试Thread.isAlive

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了测试Thread.isAlive相关的知识,希望对你有一定的参考价值。

public class TestThread extends Thread {
    @Override
    public void run() {
        super.run();
        try {
            sleep(10000);
            System.out.println("线程停止");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
public class MainThread {

    public static void main(String[] args) throws InterruptedException {
        TestThread t = new TestThread();
        t.start();
        while (true) {
            System.out.println(t.isAlive());
            Thread.sleep(1000);
        }
    }
}

结果:

true
true
true
true
true
true
true
true
true
true
线程停止
false
false
false
false
false
false

 

能正确识别线程是否在运行

以上是关于测试Thread.isAlive的主要内容,如果未能解决你的问题,请参考以下文章