[concurrency-interest] Resetting FutureTask

Joe Bowbeer joe.bowbeer at gmail.com
Tue May 23 14:57:37 EDT 2006


When the FutureTask completes it latches onto  a value or an
exception, and it's not designed to let go (i.e., reset).

Either your app needs to be able to deal with failures of this nature.
 Or your Callable needs to be designed not to fail -- using its own
retry mechanism, for example, or returing a special FAILURE value.
(If your value source is anything like a database, I suggest you write
your app to deal with the failures -- or timeouts, at least.)

A third approach would be to add a custom Future to the cache, create
and schedule separate FutureTask(s) to produce a value, and only set
the cached Future's value after a good value has been obtained from
one of the FutureTasks.  This approach might appear to "hang" in the
worst case.  Doug sketches the custom Future part in the FAQ he sent
recently.

--Joe

On 5/23/06, Tutika Chakravarthy <chakritsus at yahoo.com> wrote:
> 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
>



More information about the Concurrency-interest mailing list