【发布时间】:2017-03-09 23:09:23
【问题描述】:
我正在阅读一些 javadoc,我遇到了this example from ThreadPoolExecutor.afterExecute(...) javadocs:
class ExtendedExecutor extends ThreadPoolExecutor {
// ...
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
if (t == null && r instanceof Future<?>) {
try {
Object result = ((Future<?>) r).get();
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null)
System.out.println(t);
}
}
谁能解释一下代码如何通过第一个 if。我的意思是r 怎么可以是Future<?> 的instanceof?
【问题讨论】:
-
您能解释一下为什么您认为这不可能可能吗?
-
好吧,也许
r是RunnableFuture。 -
public class Foo implements Runnable, Future<Bar> { ... } -
非常感谢大家!
标签: java multithreading threadpool future runnable