/*
创建一个线程代码执行1000次,在线程执行过程中,中断线程实验,睡眠是为了不让子线程执行完,主线程在中断,达不到中断的效果,所以让他睡眠0.1毫秒
*/
public class Test19 {
public static void main(String[] args) {
Thread thread=new Thread(()->{
for (int i=1;i<=1000;i++) {
//判断当前线程是否中断
if(Thread.currentThread().isInterrupted()){
break;
}
System.out.println(i);
}
});
thread.start();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
//中断线程
thread.interrupt();
//判断当期线程是否中断
System.out.println(thread.isInterrupted());
}
}