优雅停止线程
{
Thread thread = new Thread(() -> {
for (int j = 0; j < 300000; j++) {
if (Thread.currentThread().isInterrupted()){
break;
}
System.out.println(j);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
});
thread.start();
Thread.sleep(300);
System.out.println("end");
thread.interrupt();
}

浙公网安备 33010602011771号