How does ConcurrentHashMap work internally?

前端 未结 6 757
傲寒
傲寒 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:24

    The "scalability issues" for Hashtable are present in exactly the same way in Collections.synchronizedMap(Map) - they use very simple synchronization, which means that only one thread can access the map at the same time.

    This is not much of an issue when you have simple inserts and lookups (unless you do it extremely intensively), but becomes a big problem when you need to iterate over the entire Map, which can take a long time for a large Map - while one thread does that, all others have to wait if they want to insert or lookup anything.

    The ConcurrentHashMap uses very sophisticated techniques to reduce the need for synchronization and allow parallel read access by multiple threads without synchronization and, more importantly, provides an Iterator that requires no synchronization and even allows the Map to be modified during interation (though it makes no guarantees whether or not elements that were inserted during iteration will be returned).

提交回复
热议问题