以下是Java Concurrency in Practice一书中让我感到困惑的片段:
interface Computable<A, V> {
V compute(A arg) throws InterruptedException;
}
public class Memoizer3<A, V> implements Computable<A, V> {
private final Map<A, Future<V>> cache
= new ConcurrentHashMap<A, Future<V>>();
private final Computable<A, V> c;
public Memoizer3(Computable<A, V> c) { this.c = c; }
public V compute(final A arg) throws InterruptedException {
Future<V> f = cache.get(arg);
if(f == null) {
Callable<V> eval = new Callable<V>() {
@Override
public V call() throws Exception {
return c.compute(arg);
}
};
FutureTask<V> ft = new FutureTask<V>(eval);
//How could it happen???
f = ft;
cache.put(arg, ft);
ft.run();
}
try {
return f.get();
} catch (InterruptedException e) {
throw launderThrowable(e.getCause());
}
}
}
如代码所示,f的类型是 Future 而ft的类型是 FutureTask。为什么我们可以将ft分配给变量f?