Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a call to a threadsafe function need to be syncronized too?

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);  
}
like image 667
Yossale Avatar asked Apr 07 '26 16:04

Yossale


1 Answers

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.

like image 85
Tom Hawtin - tackline Avatar answered Apr 09 '26 07:04

Tom Hawtin - tackline