Will there be a performance hit if I synchronize a SynchronizedMap?
For eg:
private static Map<Integer, Integer> intMap= Collections.synchronizedMap(new HashMap<Integer, Integer>());
public static int doSomething(int mapId) {
synchronized(intMap) {
Integer id = intMap.get(mapId);
if (id != null) {
//do something
}
if (id == null) {
intMap.put(mapId);
}
}
}
I have to synchronize explicitly in the above method since its a combination of operations on the synchronizedMap. Since I have to synchronize anyways, is it better to use normal hashmap instead of synchronizedMap?
Will there be a performance issue if I synchronize a synchronizedMap?