日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Example of ConcurrentHashMap in Java--转

發(fā)布時(shí)間:2025/4/5 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Example of ConcurrentHashMap in Java--转 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址: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
  • Java ConcurrentHashMap Internal Working
  • ConcurrentHashMap Example
  • ConcurrentHashMap vs Hashtable
  • ConcurrentHashMap vs HashMap

Java ConcurrentHashMap Internal Working

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.

ConcurrentHashMap Example

Find the example.?
ConcurrentHashMapDemo.java

package com.concretepage; import java.util.Iterator; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ConcurrentHashMapDemo { private final ConcurrentHashMap<Integer,String> conHashMap = new ConcurrentHashMap<Integer,String>(); public static void main(String[] args) { ExecutorService service = Executors.newFixedThreadPool(3); ConcurrentHashMapDemo ob = new ConcurrentHashMapDemo(); service.execute(ob.new WriteThreasOne()); service.execute(ob.new WriteThreasTwo()); service.execute(ob.new ReadThread()); service.shutdownNow(); } class WriteThreasOne implements Runnable { @Override public void run() { for(int i= 1; i<=10; i++) { conHashMap.putIfAbsent(i, "A"+ i); } } } class WriteThreasTwo implements Runnable { @Override public void run() { for(int i= 1; i<=5; i++) { conHashMap.put(i, "B"+ i); } }

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/p/6018203.html

總結(jié)

以上是生活随笔為你收集整理的Example of ConcurrentHashMap in Java--转的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。