[concurrency-interest] Implementing Cache

Joe Bowbeer joe.bowbeer at gmail.com
Fri May 12 04:53:03 EDT 2006


On 5/12/06, Tutika Chakravarthy <chakritsus at yahoo.com> wrote:
>  I would like to know how put method in Cache can be
>  implemented using FutureTask .
>

If you wanted to add a "put" method in order to seed the cache, you
might implement it as follows:

  public FutureTask<V> put(A arg, final V val) {
      Future<V> f = cache.get(arg);
      if (f == null) {
          Callable<V> eval = new Callable<V>() {
              public V call() {
                  return val;
              }
          };
          FutureTask<V> ft = new FutureTask<V>(eval);
          f = cache.putIfAbsent(arg, ft);
          if (f == null) {
              f = ft;
              ft.run();
          }
      }
      return f;
  }

Note that the FutureTask "trick" is useful when computing a new value
for the cache can take a long time.  If the computation time is not an
issue, however, then you can cache the raw values directly rather than
using a FutureTask (which can be thought of as a promise to produce a
value).

If you're using raw values, then your cache could be an instance of
ConcurrentHashMap and your API could be as simple as cache.get(key)
and cache.put(key, val).

Also, it might help you to know that the memo-cache example was
covered in the Bowbeer/Holmes talk at JavaOne 2005, and I believe it
was also covered in the Goetz/Holmes talk.  Both talks are viewable
online.

http://java.sun.com/javaone/sf/speaker_awards.jsp
 http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-3423.html
 http://developers.sun.com/learning/javaoneonline/2005/coreplatform/TS-5807.html


On 5/12/06, Tutika Chakravarthy <chakritsus at yahoo.com> wrote:
> Hi All,
> I have seen the sample code to implement cache using
> FutureTask and ConcuurentHashMap in one of the threads
> in this mailing list :
>
> code snippet :
>
> public interface Computable<A, V> {
>      V compute(A arg) throws Exception;
> }
>
> 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;
> }
>
>  I would like to know how put method in Cache can be
>  implemented using FutureTask .
>
>  My put method will be put(key,value).
>
>  I tried creating a dummy task with callable object
>  which returns the value we passed to it  and  it
>  worked fine.
>
>  Is there any good way of implementing the put method?
>
>
>  Tutika
>



More information about the Concurrency-interest mailing list