Im using a ConcurrentHashMap in a multi-threaded program. The map maps ServerIDs to objects containing more information about the server (whether it is online or not, how much it has been used recently, etc.). Both the ServerID and the ServerInformation are immutable.
To update the server information I am doing more or less what is proposed as point B) in this question: What is the preferred way to modify a value in ConcurrentHashMap?
namely (modified to use my own variable names) this:
public void addUsage(ServerID id, long moreUsage) {
ServerInfo oldInfo = serverMap.get(id);
ServerInfo newInfo = oldInfo.addUsage(moreUsage);
serverMap.put(id, newInfo);
}
Now my question is: Shouldn't this method be synchronized to rule out the possibility of lost updates?
Or is there another way to achieve that? Maybe something like the following (edited from my original version to remove an obvious mistake):
public void addUsage(ServerID id, long moreUsage) {
ServerInfo oldInfo = serverMap.get(id);
ServerInfo newInfo = oldInfo.addUsage(moreUsage);
while (!serverMap.replace(id, oldInfo, newInfo) ) {
oldInfo = serverMap.get(id);
newInfo = oldInfo.addUsage(moreUsage);
// try again later
Thread.sleep(SOME_TIME);
};
}