Hello.
I have recently inherited some code that uses hazelcast and I am trying to clean it up a bit.
I encountered one piece of code which looks a bit like the one below (I have simplified it a bit).
public class Cache
{
private final IMap<Key, Value> map;
public Cache(IMap map) { this.map = map; }
public void put(Key key, Value value)
{
map.lock(key);
try
{
map.put(key, value);
}
finally
{
map.unlock(key);
}
}
public Value get(Key key)
{
return map.get(key);
}
public int size()
{
return map.size();
}
public void clear()
{
map.clear();
}
}
I don't understand what the purpose is for locking around the put operation. I get the feeling it serves no purpose, but it is just that, a feeling.
Why would they lock only around the put operation and not for example around the clear operation, if locking should be used at all here.
I mean, even with locking, any client code that first performs a put and immediately after a get cannot rely on getting back the value which was just put since another thread could have modified the map.
Can anyone give me their thoughts?
If necessary I could probably supply more context to the code.
Best Regards
Thomas