java - In Brian Goetz Concurrency In Practice, why is there a while(true) in the final example of a scalable cache? -
in code listing 5.19 of brian goetz book concurrency in practice, presents finished thread safe memoizer class.
i thought understood code in example, except don't understand
while ( true )
is @ start of
public v compute(final arg) throws interruptedexception
method.
why code need while loop?
here entire code sample
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 arg) throws interruptedexception { while (true) { future<v> f = cache.get(arg); if (f == null) { callable<v> eval = new callable<v>() { public v call() throws interruptedexception { return c.compute(arg); } }; futuretask<v> ft = new futuretask<v>(eval); f = cache.putifabsent(arg, ft); if (f == null) { f = ft; ft.run(); } } try { return f.get(); } catch (cancellationexception e) { cache.remove(arg, f); } catch (executionexception e) { throw launderthrowable(e.getcause()); } } } }
eternal loop retries on cancellationexception. if other exception being thrown execution stopped.
biotext dot org has blog entry on same issue.
Comments
Post a Comment