java concurrenthashmap和hashmap的区别
答案:2 悬赏:30
解决时间 2021-04-07 11:13
- 提问者网友:醉归
- 2021-04-06 18:37
java concurrenthashmap和hashmap的区别
最佳答案
- 二级知识专家网友:湫止没有不同
- 2021-04-06 19:39
最大的区别就是ConcurrentHashMap是线程安全的,hashMap不是线程安全的。
为什么线程安全呢:
ConcurrentHashMap代码中可以看出,它引入了一个“分段锁”的概念,具体可以理解为把一个大的Map拆分成N个小的HashTable,根据key.hashCode()来决定把key放到哪个HashTable中。
在ConcurrentHashMap中,就是把Map分成了N个Segment,put和get的时候,都是现根据key.hashCode()算出放到哪个Segment中:
为什么线程安全呢:
ConcurrentHashMap代码中可以看出,它引入了一个“分段锁”的概念,具体可以理解为把一个大的Map拆分成N个小的HashTable,根据key.hashCode()来决定把key放到哪个HashTable中。
在ConcurrentHashMap中,就是把Map分成了N个Segment,put和get的时候,都是现根据key.hashCode()算出放到哪个Segment中:
全部回答
- 1楼网友:桑稚给你看
- 2021-04-06 20:02
前者是
a hash table supporting full concurrency of retrievals and
adjustable expected concurrency for updates. this class obeys the
same functional specification as hashtable, and
includes versions of methods corresponding to each method of
hashtable. however, even though all operations are
thread-safe, retrieval operations do not entail locking,
and there is not any support for locking the entire table
in a way that prevents all access. this class is fully
interoperable with hashtable in programs that rely on its
thread safety but not on its synchronization details.
后者是
hash table based implementation of the map interface. this
implementation provides all of the optional map operations, and permits
null values and the null key. (the hashmap
class is roughly equivalent to hashtable, except that it is
unsynchronized and permits nulls.) this class makes no guarantees as to
the order of the map; in particular, it does not guarantee that the order
will remain constant over time.
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯