我们知道很多类库中的阻塞方法在抛出InterruptedException后会清除线程的中断状态(例如 sleep、 阻塞队列的take),但是今天却发现了一个特别奇怪的现象,先给出代码:
public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { System.out.println("first interrupted!!!"); } System.out.println(Thread.currentThread().isInterrupted()); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { System.out.println("second interrupted!!!"); } } }); executor.shutdownNow(); }
如果按照我的理解,调用shutdownNow后会给线程池中的工作者线程发出中断请求,并在第一个睡眠的地方抛出 InterruptedException ,但是在抛出异常后这种中断状态就应该被清除了,所以第二次睡眠不应该失败,但是结果却是失败的,本来以为是系统的原因,可是在linux下测试也是如此。
更令人不解的是如果我把第二次睡眠换成其他的阻塞方法(queue.take)那么就不会抛出异常,而是正常阻塞。
public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); final BlockingQueue<String> queue = new LinkedBlockingQueue<>(); executor.execute(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { System.out.println("first interrupted!!!"); } System.out.println(Thread.currentThread().isInterrupted()); try { queue.take(); } catch (InterruptedException e) { System.out.println("second interrupted!!!"); } } }); executor.shutdownNow(); }
还有更让人受不了的,如果我们在任务最开始随便写点什么,就会按照我们期待的执行了(心中一万只草泥马呀)
public static void main(String[] args) throws InterruptedException { ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(new Runnable() { @Override public void run() { System.out.println("xxxx"); try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { System.out.println("first interrupted!!!"); } System.out.println(Thread.currentThread().isInterrupted()); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { System.out.println("second interrupted!!!"); } } }); executor.shutdownNow(); }
以后有机会再研究吧,可能还是知识不够吧!!!!