java并发之hashmap

:2019年05月26日 Java架构研究室
分享到:

在Java开发中经常会使用到hashmap,对于hashmap又了解多少,经常听到的一句话是hashmap是线程不安全的,那为什么是线程不安全的,如何才能保证线程安全,JDK又给我们提供了那些线程安全的类,这些问题是...

在Java开发中经常会使用到hashmap,对于hashmap又了解多少,经常听到的一句话是hashmap是线程不安全的,那为什么是线程不安全的,如何才能保证线程安全,JDK又给我们提供了那些线程安全的类,这些问题是今天讨论的问题,

一、hashmap为什么线程不安全

说到hashmap为什么线程不安全,首先要理解线程安全的定义。简单来讲,指的就是两个以上的线程操作同一个hashmap对象,不会发生资源争抢,hashmap中的数据不会错乱。根据以上的说法,我们大体上看下hashmap的源码,分析下其常用方法put、get的源码。

1、hashmap定义(基于JDK1.8)

经常使用hashmap的方式如下,

HashMap map1=new HashMap();

使用最简单粗暴的方式创建一个HashMap的对象,那么在底层是如何创建的,查看源码如下,

/**

* Constructs an empty <tt>HashMap</tt> with the default initial capacity

* (16) and the default load factor (0.75).

*/

public HashMap() {

this.loadFactor = DEFAULT_LOAD_FACTOR; // all other fields defaulted

根据上面的提示,可以直到使用这个构造方法创建的对象,其默认初始容量为16,默认的加载因子为0.75。此构造方法就是给loadFactor赋值,赋的为DEFAULT_LOAD_FACTOR其值为0.75,下面看下其一些属性

/**

* The default initial capacity - MUST be a power of two.

*/

static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

/**

* The maximum capacity, used if a higher value is implicitly specified

* by either of the constructors with arguments.

* MUST be a power of two <= 1<<30.

*/

static final int MAXIMUM_CAPACITY = 1 << 30;

/**

* The load factor used when none specified in constructor.

*/

static final float DEFAULT_LOAD_FACTOR = 0.75f;

可以看到默认初始容量为(DEFAULT_INITIAL_CAPACITY)1<<4,即1向左移4为,得出为1*2的4次方,为16。下面看还有最大容量(MAXIMUM_CAPACITY)为1<<30,即1向左移30位,得出为1*2的30次方。下面是默认的负载因子。从上面可以得出HashMap是又最大容量限制的,只不过平时使用的时候很少突破其最大容量(突破了就内存溢出了)。其他的构造函数暂时不看,看其put方法,

public V put(K key, V value) {

return putVal(hash(key), key, value, false, true);

}

调用了putVal方法,

final V putVal(int hash, K key, V value, boolean onlyIfAbsent,

boolean evict) {

Node<K,V>[] tab; Node<K,V> p; int n, i;

if ((tab = table) == null || (n = tab.length) == 0)

n = (tab = resize()).length;

if ((p = tab[i = (n - 1) & hash]) == null)

tab[i] = newNode(hash, key, value, null);

else {

Node<K,V> e; K k;

if (p.hash == hash &&

((k = p.key) == key || (key != null && key.equals(k))))

e = p;

else if (p instanceof TreeNode)

e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);

else {

for (int binCount = 0; ; ++binCount) {

if ((e = p.next) == null) {

p.next = newNode(hash, key, value, null);

if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st

treeifyBin(tab, hash);

break;

}

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

break;

p = e;

}

}

if (e != null) { // existing mapping for key

V oldValue = e.value;

if (!onlyIfAbsent || oldValue == null)

e.value = value;

afterNodeAccess(e);

return oldValue;

}

}

++modCount;

if (++size > threshold)

resize();

afterNodeInsertion(evict);

return null;

}

以上便是put方法的全部,代码逻辑暂不分析,从 代码中看不到任何有关并发方面的限制,比如使用synchronized关键字、使用锁、CAS等,那么在多个线程同时操作HashMap对象的时候势必会引起线程安全的问题。下面是其get方法

public V get(Object key) {

Node<K,V> e;

return (e = getNode(hash(key), key)) == null ? null : e.value;

}

调用了getNode方法,

final Node<K,V> getNode(int hash, Object key) {

Node<K,V>[] tab; Node<K,V> first, e; int n; K k;

if ((tab = table) != null && (n = tab.length) > 0 &&

(first = tab[(n - 1) & hash]) != null) {

if (first.hash == hash && // always check first node

((k = first.key) == key || (key != null && key.equals(k))))

return first;

if ((e = first.next) != null) {

if (first instanceof TreeNode)

return ((TreeNode<K,V>)first).getTreeNode(hash, key);

do {

if (e.hash == hash &&

((k = e.key) == key || (key != null && key.equals(k))))

return e;

} while ((e = e.next) != null);

}

}

return null;

}

从上面的代码也未看到有关线程并发安全方面的处理。其他方法不一一列举,我们知道HashMap是线程不安全的。

二、如何保证HashMap的线程安全

从上面的分析,我们知道在对HashMap进行添加/取出操作时未进行线程安全的控制,为了使HashMap是线程安全的,我们可以在对HashMap进行操作时枷锁,使用synchronized关键字或者可重入锁,

1、synchronized关键字

为HashMap的操作加synchronized关键字以保证其线程安全,由于synchronized有两种用法,即可以使用代码块及作用于方法上,这里演示作用于方法上,

HashMap map1=new HashMap();

public  synchronized void putMap() {

map1.put("test", "test");

}

synchronized关键字的用法可以再复习下哦。

2、可重入锁

使用ReentrantLock可重入锁控制hashMap的插入。

HashMap map1=new HashMap();

public void putMapUseLock() {

ReentrantLock rl=new ReentrantLock();

try{

rl.lock();

map1.put("test", "test");

}finally {

rl.unlock();

}

}

以上时两种解决HashMap线程不安全的解决思路,那么JDK是否提供了类似的解决方案那。

三、JDK提供的线程安全的HashMap

由于HashMap使用的范围很广,所以JDK提供了线程安全的HashMap,说两个常用的ConcurrentHashMap和synchronizedMap,这两个都是线程安全的,但其实现原理不尽相同。

1、synchronizedMap

synchronizedMap是Collections类的静态内部类,使用方法如下,

HashMap map1=new HashMap();

Map map=Collections.synchronizedMap(map1);

使用其静态方法synchronizedMap,传入一个Map对象

public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {

return new SynchronizedMap<>(m);

}

返回的synchronizedMap对象,其构造方法如下,

private final Map<K,V> m;     // Backing Map

final Object      mutex;        // Object on which to synchronize

SynchronizedMap(Map<K,V> m) {

this.m = Objects.requireNonNull(m);

mutex = this;

}

第一步判断参数m是否为null,第二步把this赋值给mutex,那么mutex代表什么意思。下面看其一个put操作,

public V put(K key, V value) {

synchronized (mutex) {return m.put(key, value);}

}

看到上面的方法,想必都很惊讶,使用了synchronized代码块,而mutex相当于共享的对象,调用的还是参数m的put方法,所以这里如果m的指向为不安全的HashMap,那么加上synchronized之后便是安全的。

get方法如下,

public V get(Object key) {

synchronized (mutex) {return m.get(key);}

}

总结下来,SynchronizedMap是使用Synchronized关键字实现的。

2、ConcurrentHashMap

通过这个类的名字,可以看出其在java.util.concurrent包下,且是为HashMap提供并发操作的类。其使用方式如下,

ConcurrentHashMap map2=new ConcurrentHashMap();

下面看其构造方法,

/**

* Creates a new, empty map with the default initial table size (16).

*/

public ConcurrentHashMap() {

}

很简洁,通过注释可得知构造一个空的Map,其容量为16,和HashMap是一样的,但是这里没有负载因子。再看其他的构造方法

看下其put/get方法

public V put(K key, V value) {

return putVal(key, value, false);

}

/** Implementation for put and putIfAbsent */

final V putVal(K key, V value, boolean onlyIfAbsent) {

if (key == null || value == null) throw new NullPointerException();

int hash = spread(key.hashCode());

int binCount = 0;

for (Node<K,V>[] tab = table;;) {

Node<K,V> f; int n, i, fh;

if (tab == null || (n = tab.length) == 0)

tab = initTable();

else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) {

if (casTabAt(tab, i, null,

new Node<K,V>(hash, key, value, null)))

break;                   // no lock when adding to empty bin

}

else if ((fh = f.hash) == MOVED)

tab = helpTransfer(tab, f);

else {

V oldVal = null;

synchronized (f) {

if (tabAt(tab, i) == f) {

if (fh >= 0) {

binCount = 1;

for (Node<K,V> e = f;; ++binCount) {

K ek;

if (e.hash == hash &&

((ek = e.key) == key ||

(ek != null && key.equals(ek)))) {

oldVal = e.val;

if (!onlyIfAbsent)

e.val = value;

break;

}

Node<K,V> pred = e;

if ((e = e.next) == null) {

pred.next = new Node<K,V>(hash, key,

value, null);

break;

}

}

}

else if (f instanceof TreeBin) {

Node<K,V> p;

binCount = 2;

if ((p = ((TreeBin<K,V>)f).putTreeVal(hash, key,

value)) != null) {

oldVal = p.val;

if (!onlyIfAbsent)

p.val = value;

}

}

}

}

if (binCount != 0) {

if (binCount >= TREEIFY_THRESHOLD)

treeifyBin(tab, i);

if (oldVal != null)

return oldVal;

break;

}

}

}

addCount(1L, binCount);

return null;

}

方法名称和HashMap是一样的,从putVal方法中看到了synchronized关键字,即也是使用synchronized关键字实现,但是肯定比synchronizedMap要高效,具体实现逻辑,暂时不分析。

综述,分析了Java中使用广泛的HashMap的常用用法及线程安全。下一篇重点分析下hashmap的源码,感兴趣的朋友可以关注一下下方公众号,更多干货好文等你来撩!

[我要纠错]
文:宋聪乔&发表于江苏
关键词: Java 开发 经常 会使 用到

来源:本文内容搜集或转自各大网络平台,并已注明来源、出处,如果转载侵犯您的版权或非授权发布,请联系小编,我们会及时审核处理。
声明:江苏教育黄页对文中观点保持中立,对所包含内容的准确性、可靠性或者完整性不提供任何明示或暗示的保证,不对文章观点负责,仅作分享之用,文章版权及插图属于原作者。

点个赞
0
踩一脚
0

您在阅读:java并发之hashmap

Copyright©2013-2024 JSedu114 All Rights Reserved. 江苏教育信息综合发布查询平台保留所有权利

苏公网安备32010402000125 苏ICP备14051488号-3南京思必达教育科技有限公司版权所有

南京思必达教育科技有限公司版权所有   百度统计

最热文章
最新文章
  • 卡尔蔡司镜片优惠店,镜片价格低
  • 苹果原装手机壳