Reputation: 505
I want to avoid duplicate key in my Hazelcast, so I am planning to use putIfAbsent. But before I perform the test, I would like to know is IMap still thread safe if my application is hosted on two servers? For example, I have my application hosted on two servers and assume both applications receive the same key and both try to use putIfAbsent to insert.
Upvotes: 1
Views: 2240
Reputation: 3150
Yes, it is thread-safe - see http://docs.hazelcast.org/docs/3.10.4/manual/html-single/index.html#locking-maps or http://docs.hazelcast.org/docs/3.10.4/javadoc/com/hazelcast/core/IMap.html
If you do putIfAbsent("hello","world")
from any two places, at most one will succeed.
Any two places could be two threads in the same JVM, two threads in two JVMs, whatewver.
You don't need to do any kind of locking to ensure this, it's handled for you.
Upvotes: 4