ConcurrentHashMap

在JDK1.5中,新增了concurrent包,从此Map就安全了

ConcurrentHashMap具体怎么实现线程安全的呢,肯定不可能是每个方法加synchronizid,那样就变成了HashTable

如图

ConcurrentHashMap引入了一个"分段锁"的概念,具体可以理解为把Map拆分成N个小的HashTable,然后根据key.hashCode()来决定把key放入哪个HashTable中

在ConcurrentHashMap中就是把Map分成N个Segment,put和get时,就是根据key.hashCode()算出放在哪个Segment中

原文地址:https://www.cnblogs.com/gudulijia/p/5673615.html