[concurrency-interest] Proper workaround for FutureTask.set()/get() race (CR 7132378)

Roel Spilker r.spilker at topdesk.com
Mon Apr 23 15:17:45 EDT 2012

Why not

public T setIfNotSet(T val);

And return the set value or null it it wasn't set. That would be more 
consistent with putIfAbsent.

Actually, I would have preferred if putIfAbsent would either return the 
value already in there or its value from the parameter if it wasn't so I 
would have been able to write:

return map.putIfAbsent(key, calculateValue());

instead of

T value = calculateValue();
T val = map.putIfAbsent(key, value);
return val != null ? val : value;


On 23-4-2012 20:22, Gregg Wonderly wrote:
> If both patterns need to be supported, than make the API support both literally.
> public boolean setIfNotSet( T val );
> public void set( T val ) throws ValueAlreadySetException;

More information about the Concurrency-interest mailing list