public class InterruptTest { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread() { @Override public void run() { while (true) { // 判断是否中断 if (Thread.currentThread().isInterrupted()) { System.out.println("interrupted"); break; } try { // 若在线程 sleep 过程中发生中断,会抛出中断异常 Thread.sleep(5000); } catch (InterruptedException e) { System.out.println("Sleep"); // 再次中断 Thread.currentThread().interrupt(); // e.printStackTrace(); } Thread.yield(); } } }; thread.start(); System.out.println("start"); Thread.sleep(3000); // 线程中断 thread.interrupt(); } }
Interrupt
最新推荐文章于 2022-03-24 16:08:14 发布