/** * 抛出InterruptedException异常的时候,要注意中断标志位 */ public class HasInterruptException { private static class UseThread extends Thread{ public UseThread(String name) { super(name); } @Override public void run() { String threadName = Thread.currentThread().getName(); while(!isInterrupted()) { try { Thread.sleep(300); } catch (InterruptedException e) { System.out.println(threadName + " catch InterruptedException interruptFlag is " + isInterrupted()); interrupt(); } System.out.println(threadName); } System.out.println(threadName + " interruptFlag is " + isInterrupted()); } } public static void main(String[] args) throws InterruptedException { Thread endThread = new UseThread("HasInterruptEx"); endThread.start(); Thread.sleep(1000); endThread.interrupt(); } }
线程抛出InterruptedException,如何中断
最新推荐文章于 2021-11-05 21:23:33 发布