java concurrentmap 用法_ConcurrentMap.putIfAbsent(key,value)用法实例

本文研究的主要是ConcurrentMap.putIfAbsent(key,value)用法的相关内容,具体如下。

业务上经常会遇到有这种场景,全局维护一个并发的ConcurrentMap, Map的每个Key对应一个对象,这个对象需要只创建一次。如果Map中该key对应的value不存在则创建,否则直接返回。

我们先看一下代码:

public static Locale getInstance(String language, String country,

String variant) {

//...

String key = some_string;

Locale locale = map.get(key);

if (locale == null) {

locale = new Locale(language, country, variant);

map.put(key, locale);

}

return locale;

}

这段代码要做的事情是:

调用 map.get(key) 方法,判断 map 里面是否有该 key 对应的 value (Locale 对象)。

如果返回 null,表示 map 里面没有要查找的 key-value mapping。new 一个 Locale 对象,并把 new 出来的这个对象与 key 一起放入 map。

最后返回新创建的 Locale 对象

我们期望每次调用 getInstance 方法时要保证相同的 key 返回同一个 Local 对象引用。这段代码能实现这个需求吗?

答案是:在单线程环境下可以满足要求,但是在多线程环境下会存在线程安全性问题,即不能保证在并发的情况相同的 key 返回同一个 Local 对象引用。

这是因为在上面的代码里存在一个习惯被称为 put-if-absent 的操作 [1],而这个操作存在一个 race condition:

if (locale == null) {

locale = new Locale(language, country, variant);

map.put(key, locale);

}

因为在某个线程做完 locale == null 的判断到真正向 map 里面 put 值这段时间,其他线程可能已经往 map 做了 put 操作,这样再做 put 操作时,同一个 key 对应的 locale 对象被覆盖掉,最终 getInstance 方法返回的同一个 key 的 locale 引用就会出现不一致的情形。所以对 Map 的 put-if-absent 操作是不安全的(thread safty)。

为了解决这个问题,java 5.0 引入了 ConcurrentMap 接口,在这个接口里面 put-if-absent 操作以原子性方法putIfAbsent(K key, V value) 的形式存在。正如 javadoc 写的那样:

putIfAbsent方法主要是在向ConcurrentHashMap中添加键—值对的时候,它会先判断该键值对是否已经存在。

如果不存在(新的entry),那么会向map中添加该键值对,并返回null。

如果已经存在,那么不会覆盖已有的值,直接返回已经存在的值。

对上面方法进行改造:

public static Locale getInstance(String language, String country,

String variant) {

//...

String key = some_string;

Locale locale = map.get(key);

if (locale == null) {

locale = new Locale(language, country, variant);

map.putIfAbsent(key, locale);

}

return locale;

}

这段代码使用了 Map 的 concurrent 形式(ConcurrentMap、ConcurrentHashMap),并简单的使用了语句map.putIfAbsent(key, locale)。这同样不能保证相同的 key 返回同一个 Locale 对象引用。

这里的错误出在忽视了 putIfAbsent 方法是有返回值的,并且返回值很重要。

所以,使用 putIfAbsent 方法时切记要对返回值进行判断。

public static Locale getInstance(String language, String country,

String variant) {

//...

String key = some_string;

Locale locale = map.get(key);

if (locale == null) {

locale = new Locale(language, country, variant);

Locale tmp = map.putIfAbsent(key, locale);

if (tmp != null) {

locale = tmp;

}

}

return locale;

}

【实例1】

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

public class Test {

public static void main(String[] args) {

//测试一下currentHashMap.putIfAbsent()

Map clientMap = new ConcurrentHashMap<>();

System.out.println("首先打印空的clientMap");

System.out.println("clientMap: " + clientMap);

System.out.println();

//在空的clientMap中添加一个新的记录

System.out.println("在空的clientMap中添加一个新的记录");

System.out.println("添加之前的clientMap: " + clientMap);

long netId = 1234567L;

String str1 = "michael";

String result = clientMap.putIfAbsent(netId, str1);

System.out.println("添加之后的clientMap: " + clientMap);

System.out.println("查看返回值result: " + result);

System.out.println();

//重复添加

System.out.println("重复添加上一次的记录");

System.out.println("添加之前的clientMap: " + clientMap);

String result2 = clientMap.putIfAbsent(netId, str1);

System.out.println("添加之后的clientMap: " + clientMap);

System.out.println("查看返回值result: " + result2);

System.out.println();

}

}

bf04ffb6fa8d73b1db1d40fb6139bb2b.png

总结

以上就是本文关于ConcurrentMap.putIfAbsent(key,value)用法实例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值