转载:https://blog.csdn.net/qq_31493821/article/details/78855069

HashMap为什么线程不安全

导致HashMap线程不安全的原因可能有两种:

1、当多个线程同时使用put方法添加元素的时候,正巧存在两个put的key发生了碰撞(根据hash值计算的bucket一样),那么根据HashMap的存储原理,这两个key会添加多数组的同一个位置,这样一定会导致其中一个线程put的数据被覆盖丢失

2、当多个线程同时检测到元素个数超过哈希表的size*loadFloat的时候,这样会发生多个线程同时对Node数组进行扩容的操作(java1.8中HashMap使用Node实体来存放内容),都在重新计算元素位置以及拷贝数据,但最后只能有一个线程能成功的将扩容后的数组赋值给table,也就是说其他线程的都会丢失,并且各自线程的数据也会丢失。关于hashMap线程不安全这一点,《java并发编程的艺术》一书中是这么描述的:“hashMap在并发执行put操作会引起死循环,导致CPU利用率接近100%。因为多线程会导致HashMap的Node链表形成环数据结构,一旦形成环数据结构,Node的next节点永远不为空,就会在获取Node时产生死循环”。由此可知,HashMap的死循环是发生在扩容时。关于hashMap的死循环可参看一下文章:

hashMap在java并发中如何发生死循环
How dose a Hashmap work in java

何如使用线程安全的HashMap
实现线程安全的方式有三种,分别是使用HashTable、Collections.SynchronizeMap、ConcurrentHashMap。

我们先分别来看看三种数据结构的部分源码:

hashtable

public synchronized V get(Object key) {
Entry<?,?> tab[] = table;
int hash = key.hashCode();
int index = (hash & 0x7FFFFFFF) % tab.length;
for (Entry<?,?> e = tab[index] ; e != null ; e = e.next) {
if ((e.hash == hash) && e.key.equals(key)) {
return (V)e.value;
}
}
return null;
}
public synchronized V put(K key, V value) { // Make sure the value is not null if (value == null) { throw new NullPointerException(); } // Makes sure the key is not already in the hashtable. Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> entry = (Entry<K,V>)tab[index]; for(; entry != null ; entry = entry.next) { if ((entry.hash == hash) && entry.key.equals(key)) { V old = entry.value; entry.value = value; return old; } } addEntry(hash, key, value, index); return null; } publicsynchronizedV remove(Object key) { Entry<?,?> tab[] = table; int hash = key.hashCode(); int index = (hash & 0x7FFFFFFF) % tab.length; @SuppressWarnings("unchecked") Entry<K,V> e = (Entry<K,V>)tab[index]; for(Entry<K,V> prev = null ; e != null ; prev = e, e = e.next) { if ((e.hash == hash) && e.key.equals(key)) { modCount++; if (prev != null) { prev.next = e.next; } else { tab[index] = e.next; } count--; V oldValue = e.value; e.value = null; return oldValue; } } return null; }
SynchronizeMap

public V put(K key, V value) {
synchronized (mutex) {return m.put(key, value);}
}
public V remove(Object key) {
synchronized (mutex) {return m.remove(key);}
}
public void putAll(Map<? extends K, ? extends V> map) {
synchronized (mutex) {m.putAll(map);}
}
public void clear() {
synchronized (mutex) {m.clear();}
}
通过这部分源码可知,HashTable、Collections.SynchronizeMap都是通过对读写进行加锁操作来保证线程的安全,一个线程进行读写数据,其余线程等等,可想而知,性能可想而知。

ConcurrentHashMap

在java8中,ConcurrentHashMap摒弃了Segment,启用了一种新的算法实现CAS。关于ConcurrentHashMap工作机制请参考深入浅出ConcurrentHashMap1.8

下面我们通过一些代码来验证他们之前的线程安全以及效率问题:

package com.iresearch.idata.common.util.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;

/**
* @author: htc
* @date: Created in 18:18 2017/12/20.
*/
public class ConcurrentMapWithMap {
private static Map<String, Long> mapWordCounts = new HashMap<>();
private static ConcurrentMap<String, Long> concurrentMapWordCounts = new ConcurrentHashMap<>();
public static Logger logger = LoggerFactory.getLogger(ConcurrentMapWithMap.class);
public static int count = 0;

public static long mapIncrease(String word) {
Long oldValue = mapWordCounts.get(word);
Long newValue = (oldValue == null) ? 1L : oldValue + 1;
mapWordCounts.put(word, newValue);
return newValue;
}

public static long ConcurrentMapIncrease(String word) {
Long oldValue, newValue;
while (true) {
oldValue = concurrentMapWordCounts.get(word);
if (oldValue == null) {
// Add the word firstly, initial the value as 1
newValue = 1L;
if (concurrentMapWordCounts.putIfAbsent(word, newValue) == null) {
break;
}
} else {
newValue = oldValue + 1;
if (concurrentMapWordCounts.replace(word, oldValue, newValue)) {
break;
}
}
}
return newValue;
}

public static void mapWordCount() throws InterruptedException, ExecutionException {
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.mapIncrease("work"));
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.mapIncrease("work"));
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.mapIncrease("work"));
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.mapIncrease("work"));
}
}
}).start();
}

public static void concurrentWordCount() throws InterruptedException, ExecutionException {
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.ConcurrentMapIncrease("work"));
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.ConcurrentMapIncrease("work"));
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.ConcurrentMapIncrease("work"));
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
int count = 0;
while (count++ < 10000) {
logger.info("mapIncrease num is " + ConcurrentMapWithMap.ConcurrentMapIncrease("work"));
}
}
}).start();
}

public static void main(String[] args) throws InterruptedException, ExecutionException {
ConcurrentMapWithMap.mapWordCount();
Thread.sleep(10000);
//多线程累加,每次都少于40000,故线程不安全
logger.info("final count map" + ConcurrentMapWithMap.mapWordCounts.get("work"));
ConcurrentMapWithMap.concurrentWordCount();
Thread.sleep(10000);
//多线程累加,每次都是40000
logger.info("final count concurrentMap" + ConcurrentMapWithMap.concurrentMapWordCounts.get("work"));
}
}

---------------------
作者:专属_Smile
来源:CSDN
原文:https://blog.csdn.net/qq_31493821/article/details/78855069
版权声明:本文为博主原创文章,转载请附上博文链接!

如何使用线程安全的HashMap的更多相关文章

  1. Java 非线程安全的HashMap如何在多线程中使用

    Java 非线程安全的HashMap如何在多线程中使用 HashMap 是非线程安全的.在多线程条件下,容易导致死循环,具体表现为CPU使用率100%.因此多线程环境下保证 HashMap 的线程安全 ...

  2. [集合]线程安全的HashMap

    一.一般模式下线程安全的HashMap 默认情况常用的HashMap都是线程不安全的,在多线程的环境下使用,常常会造成不可预知的,莫名其妙的错误.那么,我们如何实现一个线程安全的HashMap呢?其中 ...

  3. 非线程安全的HashMap 和 线程安全的ConcurrentHashMap

    在平时开发中,我们经常采用HashMap来作为本地缓存的一种实现方式,将一些如系统变量等数据量比较少的参数保存在HashMap中,并将其作为单例类的一个属性.在系统运行中,使用到这些缓存数据,都可以直 ...

  4. 3.3.2线程安全的HashMap

    代码:public class SysHashMao { private static Map<String,String> map= Collections.synchronizedMa ...

  5. 问题(一)---线程池,锁、堆栈和Hashmap相关

    一.线程池: 多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力. 假设一个服务器完成一项任务所需时间为:T1 创建线程时间,T2 在线程中 ...

  6. HashMap、HashTable、ConcurrentHashMap、HashSet区别 线程安全类

    HashMap专题:HashMap的实现原理--链表散列 HashTable专题:Hashtable数据存储结构-遍历规则,Hash类型的复杂度为啥都是O(1)-源码分析 Hash,Tree数据结构时 ...

  7. Java基础知识强化之集合框架笔记80:HashMap的线程不安全性的体现

    1. HashMap 的线程不安全性的体现: 主要是下面两方面: (1)多线程环境下,多个线程同时resize()时候,容易产生死锁现象.即:resize死循环 (2)如果在使用迭代器的过程中有其他线 ...

  8. 浅谈HashMap与线程安全 (JDK1.8)

    HashMap是Java程序员使用频率最高的用于映射(键值对)处理的数据类型.HashMap 继承自 AbstractMap 是基于哈希表的 Map 接口的实现,以 Key-Value 的形式存在,即 ...

  9. HashMap和Hashtable 线程安全性

    HashMap和Hashtable的比较是Java面试中的常见问题,用来考验程序员是否能够正确使用集合类以及是否可以随机应变使用多种思路解决问题.HashMap的工作原理.ArrayList与Vect ...

随机推荐

  1. solr增量数据配置说明

    转帖地址:http://www.blogjava.net/conans/articles/379546.html 以下资料整理自网络,觉的有必要合并在一起,这样方便查看.主要分为两部分,第一部分是对& ...

  2. day18-事务与连接池 5.关于不考虑事务隔离性出现的问题

  3. JavaScript的编译原理

    尽管通常将 JavaScript 归类为“动态”或“解释执行”语言,但事实上它是一门编译语言.这个事实对你来说可能显而易见,也可能你闻所未闻,取决于你接触过多少编程语言,具有多少经验.但与传统的编译语 ...

  4. Navicat MySQL安装

    1 下载安装包 点击下载安装包 2 解压安装包(解压后有三个文件) 第一个 Crack 是注册文件 第二个 chinese.rar 是汉化包 第三个 navicat_trial.exe 是安装程序 3 ...

  5. 【转】nginx location匹配规则

    转载请保留:http://www.nginx.cn/115.html location匹配命令 ~      #波浪线表示执行一个正则匹配,区分大小写~*    #表示执行一个正则匹配,不区分大小写^ ...

  6. 面试题:3年工作经验 已看1 有用 memcache和redis有什么区别

    此内容偏中高级,适合有三年经验者. 1.       java中wait和sleep有什么区别?多线程条件下如何保证数据安全? 答:最大区别是等待时wait会释放锁(乐观锁),而sleep会一直持有锁 ...

  7. 简单好用的General开发框架

    1.开篇概述 从2004年学习编程,2007年学习C#以来,做的多半都是跟数据库打交道的工作,所以也积累了很多数据库方面的知识,用过一些ORM框架,从了解掌握到自己实现,慢慢积累了很多代码,直到201 ...

  8. .replace(/-/g,"/")的用法

    /-/g正则表达式   g  代表  global    全部替换 var str1 ="2012-08-12 23:13"; str1 = str1.replace(/-/g,& ...

  9. Server.MapPath方法的应用方法

    老是忘记Server.MapPath的使用方法了,下面记录一下,以备后用:总注:Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径1.Server.MapPath(&q ...

  10. numpy中transpose和swapaxes函数讲解

    1 transpose() 这个函数如果括号内不带参数,就相当于转置,和.T效果一样,而今天主要来讲解其带参数. 我们看如下一个numpy的数组: arr=np.arange(16).reshape( ...