【面试题-4】HashMap和 ConcurrentHashMap 的区别

区别

1.HashMap 是非线程安全的,而 ConcurrentHashMap 是线程安全的
2.HashMap 的底层实现时数组+链表,而 ConcurrentHashMap的底层实现,在 1.7 的时候是采用数组+Segment+HashEntry;到了 1.8 版本则移除了 Segment,使得锁的粒度更小,采用 Synchronized+CAS+Node 来保证线程安全
3.开销上,由于 ConcurrentHashMap 需要加锁,所以对系统的资源开销比 HashMap 更大

posted @ 2021-05-16 17:25  澎拜编程  阅读(214)  评论(0)    收藏  举报