[concurrency-interest] Question: Concurrent Memoizer pattern withSoftHashMap

David Holmes dcholmes at optusnet.com.au
Tue Nov 20 19:04:54 EST 2007


Grace,

The SoftHashmap is neither concurrent nor thread-safe so I don't see how you
could use it somewhere you would use ConcurrentHashMap, without using
explicit synchronization.

Is there a reason you can't simply wrap the Future in a SoftReference
directly so that your ConcurrentHashMap cache is mapping from K ->
SoftReference<Future<V>> ? If you find a value with a null referent then you
can remove the WeakReference from the map and replace with a new one. You
might need additional expunging logic if your cache might accumulate "empty"
SoftReferences.

David Holmes

> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu
> [mailto:concurrency-interest-bounces at cs.oswego.edu]On Behalf Of Kwok,
> Grace (MSCIBARRA)
> Sent: Wednesday, 21 November 2007 9:39 AM
> To: concurrency-interest at cs.oswego.edu
> Subject: [concurrency-interest] Question: Concurrent Memoizer pattern
> withSoftHashMap
>
>
> Posting again due to posting problem earlier.
> =========================
>
> Hi all,
>
>     I am refactoring code and would like to use the Memoizer pattern
> but I need my cache to be a concurrent soft hash map instead of simply a
> ConcurrentHashMap.
>
> // recall the concurrent Memoizer pattern where cache is a
> ConcurrentHashMap.
> public V compute(final A arg) throws Exception{
>    Future<V> f = cache.get(arg);
>    if(f==null){
>       Callable<V> eval = new Callable<V>(){....}
>
>     FutureTask<V> ft = new FutureTask<V>(eval);
>     f = cache.putIfAbsent(arg, ft);
>     if(f == null){
>         f = ft;
>         ft.run();
>     }
> }
> f.get();
> }
>
>
> If I were to base soft hash map on  this,
> http://www.javaspecialists.co.za/archive/Issue098.html
> (search for title "New SoftHashMap")
>
> does anyone has any suggestion of having the Memoizer and SoftHashMap
> concepts working together such that I do not need to use explicit
> synchronization?
>
> Thanks, Grace
> --------------------------------------------------------
>
> NOTICE: If received in error, please destroy and notify sender.
> Sender does not intend to waive confidentiality or privilege. Use
> of this email is prohibited when received in error.
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>



More information about the Concurrency-interest mailing list