HashMap and concurrency- different keys

前端 未结 4 1782
一生所求
一生所求 2021-01-20 04:58

Say I have a hash map and multiple threads. If I have a synchronized method that adds to the hash map, how would I make it possible that two different threads can put differ

4条回答
  •  难免孤独
    2021-01-20 05:29

    The answer to your question is NO. Because synchronized block forces every tread to wait in the single queue. With ConcurrentHashMap you have more chances for simultaneously add, because it locks only basket where element will be inserted instead of locking whole HashMap.

提交回复
热议问题