ConcurrentHashMap使用要点
ConcurrentHashMap的简要总结:
1、public V get(Object key)不涉及到锁,也就是说获得对象时没有使用锁;
2、put、remove方法要使用锁,但并不一定有锁争用,原因在于ConcurrentHashMap将缓存的变量分到多个Segment,每个Segment上有一个锁,只要多个线程访问的不是一个Segment就没有锁争用,就没有堵塞,各线程用各自的锁,ConcurrentHashMap缺省情况下生成16个Segment,也就是允许16个线程并发的更新而尽量没有锁争用;
3、Iterator对象的使用,不一定是和其它更新线程同步,获得的对象可能是更新前的对象,ConcurrentHashMap允许一边更新、一边遍历,也就是说在Iterator对象遍历的时候,ConcurrentHashMap也可以进行remove,put操作,且遍历的数据会随着remove,put操作产出变化,所以希望遍历到当前全部数据的话,要么以ConcurrentHashMap变量为锁进行同步(synchronized该变量),要么使用CopiedIterator包装iterator,使其拷贝当前集合的全部数据,但是这样生成的iterator不可以进行remove操作。
Hashtable和ConcurrentHashMap的不同点:
1、Hashtable对get,put,remove都使用了同步操作,它的同步级别是正对Hashtable来进行同步的,也就是说如果有线程正在遍历集合,其他的线程就暂时不能使用该集合了,这样无疑就很容易对性能和吞吐量造成影响,从而形成单点。而ConcurrentHashMap则不同,它只对put,remove操作使用了同步操作,get操作并不影响,详情请看以上第1,2点,当前ConcurrentHashMap这样的做法对一些线程要求很严格的程序来说,还是有所欠缺的,对应这样的程序来说,如果不考虑性能和吞吐量问题的话,个人觉得使用Hashtable还是比较合适的;
2、Hashtable在使用iterator遍历的时候,如果其他线程,包括本线程对Hashtable进行了put,remove等更新操作的话,就会抛出ConcurrentModificationException异常,但如果使用ConcurrentHashMap的话,就不用考虑这方面的问题了,详情请看以上第3点;
1.HashMap或者ArrayList边遍历边删除数据会报java.util.ConcurrentModificationException异常
Map<Long, String> mReqPacket = new HashMap<Long, String>();
for (long i = 0; i < 15; i++) {
mReqPacket.put(i, i + "");
} for (Entry<Long, String> entry : mReqPacket.entrySet()) {
long key = entry.getKey();
String value = entry.getValue();
if (key < 10) {
mReqPacket.remove(key);
}
} for (Entry<Long, String> entry : mReqPacket.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
所以要用迭代器删除元素:
Map<Long, String> mReqPacket = new HashMap<Long, String>();
for (long i = 0; i < 15; i++) {
mReqPacket.put(i, i + "");
} for (Iterator<Entry<Long, String>> iterator = mReqPacket.entrySet().iterator(); iterator.hasNext();) {
Entry<Long, String> entry = iterator.next();
long key = entry.getKey();
if (key < 10) {
iterator.remove();
}
} for (Entry<Long, String> entry : mReqPacket.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
2.对ConcurrentHashMap边遍历边删除或者增加操作不会产生异常(可以不用迭代方式删除元素),因为其内部已经做了维护,遍历的时候都能获得最新的值。即便是多个线程一起删除、添加元素也没问题。
Map<Long, String> conMap = new ConcurrentHashMap<Long, String>();
for (long i = 0; i < 15; i++) {
conMap.put(i, i + "");
} for (Entry<Long, String> entry : conMap.entrySet()) {
long key = entry.getKey();
if (key < 10) {
conMap.remove(key);
}
} for (Entry<Long, String> entry : conMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
}
3.一个线程对ConcurrentHashMap增加数据,另外一个线程在遍历时就能获得。
static Map<Long, String> conMap = new ConcurrentHashMap<Long, String>(); public static void main(String[] args) throws InterruptedException {
for (long i = 0; i < 5; i++) {
conMap.put(i, i + "");
} Thread thread = new Thread(new Runnable() {
public void run() {
conMap.put(100l, "100");
System.out.println("ADD:" + 100);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
} });
Thread thread2 = new Thread(new Runnable() {
public void run() {
for (Iterator<Entry<Long, String>> iterator = conMap.entrySet().iterator(); iterator.hasNext();) {
Entry<Long, String> entry = iterator.next();
System.out.println(entry.getKey() + " - " + entry.getValue());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
thread.start();
thread2.start(); Thread.sleep(3000);
System.out.println("--------");
for (Entry<Long, String> entry : conMap.entrySet()) {
System.out.println(entry.getKey() + " " + entry.getValue());
} } 输出:
ADD:100
0 - 0
100 - 100
2 - 2
1 - 1
3 - 3
4 - 4
--------
0 0
100 100
2 2
1 1
3 3
4 4
ConcurrentHashMap使用要点的更多相关文章
- HashMap HashTable和ConcurrentHashMap的区别
HashMap和Hashtable都实现了Map接口,其主要的区别有:线程安全性,同步(synchronization),以及效率. HashMap和Hashtable基本上没啥区别,除了HashMa ...
- 集合03_Map
Map集合总览 保存映射关系key-value键值对,键唯一,值可以重复,Map和Set的实现类相似 Entry是Map的内部类 Map接口中常用的方法: void clear() Set keySe ...
- Java 8 中 ConcurrentHashMap工作原理的要点分析
简介: 本文主要介绍Java8中的并发容器ConcurrentHashMap的工作原理,和其它文章不同的是,本文重点分析了对不同线程的各类并发操作如get,put,remove之间是如何同步的,以及这 ...
- Java8 中 ConcurrentHashMap工作原理的要点分析
简介: 本文主要介绍Java8中的并发容器ConcurrentHashMap的工作原理,和其它文章不同的是,本文重点分析了不同线程的各类并发操作如get,put,remove之间是如何同步的,以及这些 ...
- Java 编程要点之并发(Concurrency)详解
计算机用户想当然地认为他们的系统在一个时间可以做多件事.他们认为,他们可以工作在一个字处理器,而其他应用程序在下载文件,管理打印队列和音频流.即使是单一的应用程序通常也是被期望在一个时间来做多件事.例 ...
- Java面试通关要点汇总集
Java面试通关要点汇总集 2018-03-09 转自:Java面试通关要点汇总集 文章目录 1. 基础篇 1.1. 基本功 1.2. 集合 1.3. 线程 1.4. 锁机制2. 核心篇 2 ...
- ConcurrentHashMap基于JDK1.8源码剖析
前言 声明,本文用的是jdk1.8 前面章节回顾: Collection总览 List集合就这么简单[源码剖析] Map集合.散列表.红黑树介绍 HashMap就是这么简单[源码剖析] LinkedH ...
- ConcurrentHashMap 源码阅读小结
前言 每一次总结都意味着重新开始,同时也是为了更好的开始.ConcurrentHashMap 一直是我心中的痛.虽然不敢说完全读懂了,但也看了几个重要的方法,有不少我觉得比较重要的知识点. 然后呢,放 ...
- 并发编程——ConcurrentHashMap#addCount() 分析
前言 ConcurrentHashMap 精华代码很多,前面分析了 helpTransfer 和 transfer 和 putVal 方法,今天来分析一下 addCount 方法,该方法会在 putV ...
随机推荐
- 安全-分析深圳电信的新型HTTP劫持方式
ISP的劫持手段真是花样百出,从以前的DNS(污染)劫持到后来的共享检测,无不通过劫持正常的请求来达到他们的目的. 之前分析过通过劫持HTTP会话,插入iframe来检测用户后端有无共享行为,但后来移 ...
- PHP-线程一直不释放调试
一.现象 1.查看进程是否存在 ps -ef | grep -v 'grep' |grep -E 'shell/cron/bonus/cash' www 2624 1 0 Oct24 ...
- MySQL 常用show命令
MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
- Oracle RAC 并发与架构
10g RAC进程总概 一. RAC 并发 RAC 的本质是一个数据库,运行在多台计算机上的数据库,它的主要任务是数据库就是事务处理,它通过 Distributed Lock Management(D ...
- 【android极光推送】—从客户端到后台,一文通吃
sion android:name="android.permission.VIBRATE" /> <uses-permission android:name=&quo ...
- Mysql源码目录结构
Programs for handling SQL commands. The "core" of MySQL. These are the .c and .cc files in ...
- XML序列化中含有List的情况,序列化到根节点下一层
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...
- nginx 偶发 403原因
观察errorlog 日志 是否存在类似错误 [error] 12788#0: *322 connection is denied by policyframe[return code:8], 观察是 ...
- PHP中使用CURL实现get和post请求(总结)
一.什么是curl curl是利用url语法在命令行方式下工作的开源文件传输工具. 二.PHP curl函数
- 剑指offer系列49--求1+2+...+N的和
[题目]求1+2+3+…+n, * 要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). package com.exe10.offer ...