[concurrency-interest] conditional put

Dhanji R. Prasanna dhanji at gmail.com
Tue Nov 6 01:53:34 EST 2007


Bob,

That is interesting--if I read it correctly, it is an evaluate-on-get
rather than evaluate-on-successful-put. Ignoring subsequent concurrent
puts if there is already a value...

That's not exactly what I was after but it offers a concurrent put and
defers the expense of computation, so I think it works for my needs.

Would be nice to have a callable closure in CHM.putIfAbsent() *hint*
Thank you all for responses,

Dhanji.

On 11/6/07, Bob Lee <crazybob at crazybob.org> wrote:
> I use ReferenceCache for this sort of thing:
> http://google-guice.googlecode.com/svn/trunk/src/com/google/inject/internal/
>
> We're working on a more flexible replacement though.
>
> In the mean time, you can also use a ConcurrentMap<K, Future<V>>.
>
> Future<V> future = map.get(key);
> if (future != null) {
>   return future.get();
> }
> Callable<V> callable = ...;
> Future<V> future = new FutureTask<V>(callable);
> Future<V> fromMap = map.putIfAbsent(key, future);
> return (fromMap == null ? future : fromMap).get();
>
> Bob
>
> On 11/5/07, Dhanji R. Prasanna <dhanji at gmail.com> wrote:
> > Hi,
> >
> > CHM is a wonderful abstraction for atomic conditional puts
> > (putIfAbsent). However, it does not work well in a lambda-like use
> > case (i.e. evaluate if absent).
> >
> > Is there some way for me to achieve the following:
> >
> > chm.putIfAbsent(key, new Callable<Value> {
> >     //expression that is only evaluated if key is absent
> > });
> >
> > And that this callable is evaluated atomically.
> > Thank you,
> >
> > Dhanji.
> > _______________________________________________
> > 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