我讀了一些javadoc和我遇到this example from ThreadPoolExecutor.afterExecute(...)
javadocs:Runnable instanceof Future <?>?
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);
}
}
傳來有人可以解釋我的代碼如何通過第一個如果。我的意思是r
如何可以實例化爲Future<?>
?
你能解釋一下爲什麼你認爲這不可能嗎? – ruakh
嗯,也許'r'是[RunnableFuture](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RunnableFuture.html)。 – azurefrog
'公共類Foo實現Runnable,未來 {...}' –