[concurrency-interest] memoizer cache

Zhong Yu zhong.j.yu at gmail.com
Fri Oct 21 22:49:40 EDT 2011


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