I'm confused as to how one handles concurrency with Hazelcast distributed data structures. For example, I need to be able to do the following:Value v = map.get(key);if (v.hasSomeConditionMet()){v = new Value(...);map.put(v);}Won't this result in a check-then-act race condition where two cluster members both put the new value - supposing I want only one cluster member to win?Is there any way to achieve this atomically in a way similar to putIfAbsent()?Thanks.--
You can use map.replace(key, oldValue, newValue);Value v = map.get(key);if (v.hasSomeConditionMet()){Value newV = new Value(...);map.replace(key, v, newV);}
Or you can use locks;if (map.tryLock(key)) {Value v = map.get(key);if (v.hasSomeConditionMet()){v = new Value(...);map.put(key, v);}}
If the lock is held by another thread then the current thread becomes disabled for thread scheduling purposes and lies dormant until the lock has been acquired, at which time the lock hold count is set to one.
It does block, otherwise what would you do if you need to lock an object and do something with it - hence my question about sleep/loop.
Indeed the Hazelcast lock does seem to have different semantics:
If the lock is not available then the current thread doesn't wait and returns false immediately.
So how are you dealing with the situation where it can't acquire the lock?
Thanks
--
You received this message because you are subscribed to the Google Groups "Hazelcast" group.
To post to this group, send email to haze...@googlegroups.com.
To unsubscribe from this group, send email to hazelcast+...@googlegroups.com.
Visit this group at http://groups.google.com/group/hazelcast?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.