As far as I know, java.util.Hashtable
synchronizes each and every method in the java.util.Map
interface, while Collections.synchronizedMap(hash_map)
returns a wrapper object containing synchronized methods delegating calls to the actual hash_map
(correct me if I am wrong).
I have two questions :
What difference does it make to synchronize each and every method and to have a wrapper class? What are the scenarios to choose one over the other?
What happens when we do Collections.synchronizedMap(hash_table)
? Will this be equal to simply using a normal java.util.Hashtable
?
Here are the answers I've gotten from a bit of (hopefully correct) research:
Both provide the same degree of synchronization. If you were to wrap Hashtable
through Collections.synchronized you would have the same degree, but with another redundant layer, of synchronization.
The main difference between Hashtable
and Collections.synchronizedMap(HashMap)
exist more at the API level. Because Hashtable
is part of Java's legacy code, you'll see that the Hashtable
API is enhanced to implement the Map
interface, to become part of Java's collections framework. This means that if you were to wrap Hashtable
through Collections.synchronizedMap()
, the API of the wrapped Hashtable
would become limited to the Map
API. So if the API of Hashtable
is encompassed in your definition of behavior, then it is obviously altered/limited.