public static void interrupt() throws InterruptedException {
Runnable runnable = () ->{
int num = 0;
while (num<=300){
if(num%100==0){
System.out.println(num+ "是100的倍数");
}
num++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
Thread thread = new Thread(runnable);
thread.start();
Thread.sleep(500);
thread.interrupt();
}
运行结果:
0是100的倍数
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.exercise.unsafe.InterruptTest.lambda$interrupt$0(InterruptTest.java:18)
at java.lang.Thread.run(Thread.java:748)