public class Test20 {
public static void main(String[] args) {
Thread ti = new Thread(()->{
for (int i = 1; i <=1000; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
//抛出异常
}
System.out.println(i);
}
});
ti.start();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ti.interrupt();//如果线程在休眠被中断,无法结束线程,可以抛出异常
ti.interrupt();
ti.interrupt();
ti.interrupt();
System.out.println(ti.isInterrupted());
}
}