[concurrency-interest] conditional put

kevin bourrillion kevinb at google.com
Tue Nov 6 02:41:51 EST 2007


Dhanji,

Doug and Bob and I have coincidentally been discussing this again
lately; Doug is still skeptical that a truly general-purpose reusable
solution is feasible, but I'm a damned optimist.  If we manage to come
up with something it will go into the google collections library.



On Nov 5, 2007 10:53 PM, Dhanji R. Prasanna <dhanji at gmail.com> wrote:
> 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
> > >
> >
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at altair.cs.oswego.edu
> http://altair.cs.oswego.edu/mailman/listinfo/concurrency-interest
>



-- 
Kevin Bourrillion @ Google
go/javalibraries
google-collections.googlecode.com
google-guice.googlecode.com
jsr-310.dev.java.net


More information about the Concurrency-interest mailing list