[concurrency-interest] memoizer cache

bhm bheem at sbcglobal.net
Mon Oct 24 09:06:21 EDT 2011


 Thanks everyone for comments and suggestions, I've updated code used
 same sync instance when creating post-expired entry and added another
 impl class that recurs on get and doesnt use Entry class. Thanks.

> On Fri, Oct 21, 2011 at 10:49 PM, Zhong Yu <zhong.j.yu at gmail.com> wrote:
>> I think there's a small problem
>>
>> [1]       entry = store.get(key);
>>
>>           if ( expired(entry.v) )
>>
>>               final AtomicInteger sync = entry.sync;
>>
>> [2]            if (sync.compareAndSet(0, 1)) { // otherwise retrun old value
>>                   try {
>>                       entry = (Entry<V>) store.get(key); // get again
>>                       if ( expired(entry.v) ) { // double-check
>>                           Entry<V> newval = new Entry<V>(factory.create(key));
>> [3]                       store.replace(key, newval);
>>                           ret = newval;
>>                       }
>>                   }
>>                   finally {sync.set(0);/*unlock*/}
>>
>> since you insert a new entry at [3], it's possible that two threads
>> see two different entries at [1], and they are trying to acquire
>> different locks at [2]. Both may succeed and proceed concurrently. Of
>> course the chance of this happening is very small. I don't see a
>> problem if you reuse the entry, so that [2] always sees the same sync.
>>
>> Zhong Yu
>>
>



More information about the Concurrency-interest mailing list