views:

710

answers:

1

From my understanding, each of these methods: get() and put() are atomic.

But, when accessing caches like ehcache & memcached, in a CheckThenAct fashion, race conditions will arise.

What should we synchronize on to ensure that if thread1 does a Get() and thread2 does a Get() followed by a Put(), that the first Get() will receive the new value added by thread2 ?

A: 
cletus
Jacques René Mesrine
Even with increment and decrement you have problems with setting the initial value. If the value is not an integer the increment will do nothing. If you want exact values you will need to pre-fill the values that you will be incrementing.
Miha Hribar
@Miha Good point! In the database world, I do the same thing, prefill tables so that I can row lock on them. AFAIK, you cannot row lock on a non-existent row.
Jacques René Mesrine