How does ConcurrentHashMap work internally?

前端 未结 6 748
傲寒
傲寒 2020-12-02 07:05

I was reading the official Oracle documentation about Concurrency in Java and I was wondering what could be the difference between a Collection returned by

6条回答
  •  眼角桃花
    2020-12-02 07:25

    The ConcurrentHashMap is very similar to the java.util.HashTable class, except that ConcurrentHashMap offers better concurrency than HashTable or synchronizedMap does. ConcurrentHashMap does not lock the Map while you are reading from it. Additionally,ConcurrentHashMap does not lock the entire Mapwhen writing to it. It only locks the part of the Map that is being written to, internally.

    Another difference is that ConcurrentHashMap does not throw ConcurrentModificationException if the ConcurrentHashMap is changed while being iterated. The Iterator is not designed to be used by more than one thread though whereas synchronizedMap may throw ConcurrentModificationException

提交回复
热议问题