If I'm using ConcurrentHashMap (where the put is thread safe) , and I supply a public function myPut that uses the ConcurrentHashMap put - do I need to synchronize my function?
meaning : should this be synchronized?
ConcurrentHashMap map;
public void myPut(int something) {
this.map.put(something);
}
Because the map reference is not declared final it may be changed. Therefore, there is a potential threading bug here.
If map is supposed to be a mutable reference then you will need to do some more work. Otherwise use final. Indeed, use final whenever you can, even if it is "easier" not to. "final is the new [old] private." You probably want to make map private and generic too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With