Tag: 可调用的

为什么在可调用中设置中断位

因此,该资源( http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html )建议在线程不处理中断本身时设置线程中的中断位,“ 这样,调用堆栈上方的代码可以了解中断并在需要时响应它 。“ 假设我正在使用ExecutorService在不同的Thread中运行某些东西。 我构造一个Callable并将此Callable传递给ExecutorService.submit(),它返回一个Future。 如果Callable被中断然后重置中断位,则在调用Future.get()时,关联的Future不会抛出InterruptedException。 那么,如果此Future是主线程访问生成的线程的唯一方式,那么在Callable中设置被中断位的目的是什么呢? class MyCallable implements Callable { @Override public String call() { while (!Thread.currentThread().isInterrupted()) { } Thread.currentThread().interrupt(); return “blah”; } } ExecutorService pool = makeService(); Future future = pool.submit(new MyCallable()); // Callable gets interrupted and the Callable resets the interrupt bit. future.get(); // Does not thrown an InterruptedException, […]