在Brian Goetz的书"Concurrency In Practice"的代码清单5.19中,他展示了他完成的线程安全的Memoizer类.
我以为我理解了这个例子中的代码,除了我不明白的是什么
while ( true )
Run Code Online (Sandbox Code Playgroud)
是为了开始
public V compute(final A arg) throws InterruptedException
Run Code Online (Sandbox Code Playgroud)
方法.
为什么代码需要while循环?
这是整个代码示例
public class Memoizer<A, V> implements Computable<A, V> {
private final ConcurrentMap<A, Future<V>> cache
= new ConcurrentHashMap<A, Future<V>>();
private final Computable<A, V> c;
public Memoizer(Computable<A, V> c) { this.c = c; }
public V compute(final A arg) throws InterruptedException {
while (true) {
Future<V> f = cache.get(arg);
if (f == null) {
Callable<V> eval = new Callable<V>() {
public V call() …Run Code Online (Sandbox Code Playgroud)