Do I need a synchronous call to the thread safety function?

If I use ConcurrentHashMap (where put is thread safe) and I provide a public function myPut that uses ConcurrentHashMap - do I need to synchronize my function?

Meaning: Should this be synchronized?

ConcurrentHashMap map;  
public void myPut(int something) {  
   this.map.put(something);  
}
+3
source share
5 answers

Concurrency Utilities, such as ConcurrentHashMap, are designed so that you do not need to synchronize: they will handle stream access internally.

, , , . , : ConcurrentHashMap - , , java.util.concurrentlibrary - , , , . , , , , , ( "final", , - "volatile" AtomicReference, ).

+2

map ​​final, . .

map , . final. , final , , "" . "final - [] private." , map private .

+4

ConcurrentHashMap, "put", , "puts" . concurrency ...

, , .

, ConcurrentHashTable Javadoc, ...

-Patrick

+1

.
myPut . , HashTable . (. )

0

.

, , . , , - .

, , . , .

You only need to synchronize this method if you need to do this in order to make the class stream safe for other reasons - at this point you may need to ask if the overhead is worth it ConcurrentHashMap.

0
source

All Articles