首页 > 代码库 > 测试Thread.isAlive

测试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