加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Example of ConcurrentHashMap in Java--转

发布时间:2020-12-14 06:21:59 所属栏目:Java 来源:网络整理
导读:原文地址:http://www.concretepage.com/java/example_concurrenthashmap_java On this page we will provide example of ConcurrentHashMap in java. ConcurrentHashMap is thread safe but does not use locking on complete map. It is fast and has bett

原文地址:http://www.concretepage.com/java/example_concurrenthashmap_java

On this page we will provide example of ConcurrentHashMap in java. ConcurrentHashMap is thread safe but does not use locking on complete map. It is fast and has better performance in comparison to Hashtable in concurrent environment. Find some methods of?ConcurrentHashMap.?get()?: Pass the key as an argument and it will return associated value.?put(): Pass key and value and it will map.?putIfAbsent(): Pass key and value and it will map only if key is not already present.?remove(): Removes the entry for the given key.

Contents

1.?Concurrency for retrieval: Retrieval of elements from?ConcurrentHashMap?does not use locking. It may overlap with update operation. We get the elements of last successfully completed update operation. In case of aggregate operations such as?putAll?and?clear(),concurrent retrieval may show insertion or removal of only some elements.?2.?Iteration of ConcurrentHashMap: Iterators and Enumerations also return the elements which have been concurrently added while iterating.?ConcurrentHashMap?does not throw?ConcurrentModificationException.?3.?Concurrency for updates: Concurrent updates are thread safe.?ConcurrentHashMap?constructor has an optional concurrency level argument. The default value is 16. This is the estimated number of concurrently updating threads. It is used in internal sizing to accommodate concurrently updating threads. Hash table is internally partitioned into the concurrency level number so that it can avoid updating concurrent thread contention.

Find the example.?ConcurrentHashMapDemo.java

();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读