Why there is no ConcurrentLinkedHashMap class in jdk?

后端 未结 3 2026
旧时难觅i
旧时难觅i 2021-02-14 17:45

This question follows directly from my previous question here in SO . I think the answer to my second question is no . So I would like understand why there is no ConcurrentLinke

3条回答
  •  青春惊慌失措
    2021-02-14 17:55

    #define PERSONAL_OPINION
    

    From a design point of view, it makes more sense to always have to use

    Map m = Collections.synchronizedMap(new HashMap());
      ...
    Set s = m.keySet();  // Needn't be in synchronized block
      ...
    synchronized(m) {  // Synchronizing on m, not s!
       Iterator i = s.iterator(); // Must be in synchronized block
       while (i.hasNext())
          foo(i.next());
    }
    

    example in synchronizedMap

    Why? Because the synchronization mechanism is tied to a high abstraction (the Map interface). But assuming I am right there can be two reasons to still have ConcurrentHashMap:

    • Either ConcurrentHashMap exists before this sync mechanism
    • Either there is a performance gain in creating a specific synchronized mechanism.

    My point is in the ideal world of design even ConcurrentHashMap should not exist.

    #end //personal opinion
    

提交回复
热议问题