What's the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?

前端 未结 19 895
名媛妹妹
名媛妹妹 2020-11-22 11:45

I have a Map which is to be modified by several threads concurrently.

There seem to be three different synchronized Map implementations in the Java API:

    <
19条回答
  •  清酒与你
    2020-11-22 12:08

    • Hashtable and ConcurrentHashMap do not allow null keys or null values.

    • Collections.synchronizedMap(Map) synchronizes all operations (get, put, size, etc).

    • ConcurrentHashMap supports full concurrency of retrievals, and adjustable expected concurrency for updates.

    As usual, there are concurrency--overhead--speed tradeoffs involved. You really need to consider the detailed concurrency requirements of your application to make a decision, and then test your code to see if it's good enough.

提交回复
热议问题