Java如何使用ConcurrentHashMap进行并发操作
作者:WarmHope
时间:2023-04-25
浏览:4
说明1、ConcurentHashMap结合了HashMap和Hashtable的优点。HashMap不考虑同步,Hashtable考虑同步。但是Hashtable每次同步都要锁定整个结构。2、ConcurentHashMap锁的方式是稍微细粒度的。ConcurentHashMap将hash表分成16桶(默认值),如get、put、remove等常用操作只锁定目前需要的桶。实例/***Createsanew,emptymapwiththedefaultinitialtablesize(16).*/publ
本文目录
说明
1、ConcurentHashMap结合了HashMap和Hashtable的优点。HashMap不考虑同步,Hashtable考虑同步。但是Hashtable每次同步都要锁定整个结构。
2、ConcurentHashMap锁的方式是稍微细粒度的。ConcurentHashMap将hash表分成16桶(默认值),如get、put、remove等常用操作只锁定目前需要的桶。
实例
/**
* Creates a new, empty map with the default initial table size (16).
*/
public ConcurrentHashMap() {
}
/**
* Creates a new, empty map with an initial table size
* accommodating the specified number of elements without the need
* to dynamically resize.
*
* @param initialCapacity The implementation performs internal
* sizing to accommodate this many elements.
* @throws IllegalArgumentException if the initial capacity of
* elements is negative
*/
public ConcurrentHashMap(int initialCapacity) {
if (initialCapacity < 0)
throw new IllegalArgumentException();
int cap = ((initialCapacity >= (MAXIMUM_CAPACITY >>> 1)) ?
MAXIMUM_CAPACITY :
tableSizeFor(initialCapacity + (initialCapacity >>> 1) + 1));
this.sizeCtl = cap;
}
/**
* Creates a new map with the same mappings as the given map.
*
* @param m the map
*/
public ConcurrentHashMap(Map extends K, ? extends V> m) {
this.sizeCtl = DEFAULT_CAPACITY;
putAll(m);
}
作者最新文章
漫蛙漫画官网首页登录入口
2026-10-28 19:02
mindmanager怎么使用 思维导图入门
2026-10-27 09:54
Word2016怎么移除文档空白页
2026-10-16 22:30
endnote怎么引用中文参考文献格式设置教程
2026-10-14 17:25
sql条件判断字符串不包含某个字符串的方法详解
2026-10-13 17:39
上一篇:
怎么用Java代码实现图书管理系统
下一篇:
Java如何分析汉诺塔问题
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多

































