JAVA中有两种判断线程停止的方法:
1) public static boolean interrupted(): 测试 currentThread() 是否已经中断。
2) public boolean this. isInterrupted(): 测试 this 关键字所在类的对象是否已经中断。
第一种:public static boolean interrupted(): 测试当前线程是否已经是中断状态,执行后具有清除状态标志值为 false的功能。
demo代码:
package day4;
public class Thread1 extends Thread{
@Override
public void run() {
for(int i=0;i<50000;i++) {
System.out.println("i=" + i);
}
}
public static void main(String[] args) throws Exception {
Thread1 t1 = new Thread1();
t1.start();
Thread.sleep(100);
t1.interrupt();
System.out.println("是否停止:" + t1.interrupted());
System.out.println("是否停止:" + t1.interrupted());
}
}
结果: