[concurrency-interest] Resetting FutureTask

Tutika Chakravarthy chakritsus at yahoo.com
Tue May 23 13:24:20 EDT 2006


Hi,
I have a small question about the popular Memoize
example
code snippet:
  public class Memoize<A, V> implements Computable<A,
V>
{
     public Memoize(Computable<A, V> c) {
         this.c = c;
     }
     public V compute(final A arg) throws Exception {
         Future<V> f = cache.get(arg);
         if (f == null) {
             Callable<V> eval = new Callable<V>() {
                 public V call() throws Exception {
                     return c.compute(arg);
                 }
             };
             FutureTask<V> ft = new
FutureTask<V>(eval);
             f = cache.putIfAbsent(arg, ft);
             if (f == null) { f = ft; ft.run(); }
         }
         return f.get();
     }
     private final ConcurrentMap<A, Future<V>> cache =
         new ConcurrentHashMap<A, Future<V>>();
     private final Computable<A, V> c;
}

My questions is, if call() method in Callable instance
throws Exception , How can I reset the FutureTask?

I am trying to put some code around "f.get()" like

try{
f.get()

}catch(ExecutionException e)
{
cache.remove(arg,f);

}

But will this solve the problem of reseting the
FutureTask?

If one thread gets an exception in call() method,I am
trying to avoid other threads getting the same
exception.


Tutika


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the Concurrency-interest mailing list