深入理解ConcurrentMap.putIfAbsent(key,value) 用法
转自:http://blog.csdn.net/exceptional_derek/article/details/40384659
先看一段代码:
- public class Locale {
- private final static Map<String, Locale> map = new HashMap<String,Locale>();
- 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 对象
- if (locale == null) {
- locale = new Locale(language, country, variant);
- map.put(key, locale);
- }
- /**
- * If the specified key is not already associated
- * with a value, associate it with the given value.
- * This is equivalent to
- * <pre>
- * if (!map.containsKey(key))
- * return map.put(key, value);
- * else
- * return map.get(key);</pre>
- * except that the action is performed atomically.
- * .....
- */
- public class Locale implements Cloneable, Serializable {
- private final static ConcurrentMap<String, Locale> map = new ConcurrentHashMap<String, Locale>();
- 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;
- }
- // ....
- }
- /**
- * @return the previous value associated with the specified key, or
- * <tt>null</tt> if there was no mapping for the key.
- * (A <tt>null</tt> return can also indicate that the map
- * previously associated <tt>null</tt> with the key,
- * if the implementation supports null values.)
- */
- public final class Locale implements Cloneable, Serializable {
- // cache to store singleton Locales
- private final static ConcurrentHashMap<String, Locale> cache = new ConcurrentHashMap<String, Locale>(32);
- static Locale getInstance(String language, String country, String variant) {
- if (language== null || country == null || variant == null) {
- throw new NullPointerException();
- }
- StringBuilder sb = new StringBuilder();
- sb.append(language).append('_').append(country).append('_').append(variant);
- String key = sb.toString();
- Locale locale = cache.get(key);
- if (locale == null) {
- locale = new Locale(language, country, variant);
- Locale l = cache.putIfAbsent(key, locale);
- if (l != null) {
- locale = l;
- }
- }
- return locale;
- }
- // ....
- }
- Locale l = cache.putIfAbsent(key, locale);
- if (l != null) {
- locale = l;
- }
深入理解ConcurrentMap.putIfAbsent(key,value) 用法的更多相关文章
- ConcurrentMap.putIfAbsent(key,value) 用法讨论
ConcurrentMap.putIfAbsent(key,value) 用法讨论 http://wxl24life.iteye.com/blog/1746794
- binarySearch(int[] a,int fromIndex,int toIndex, int key)的用法
package com.Summer_0420.cn; import java.util.Arrays; /** * @author Summer * binarySearch(int[] a,int ...
- 理解并掌握Promise的用法
前沿: Promise在处理异步操作非常有用.项目中,与后端进行数据请求的时候经常要用到Promise.我们可以用promise + xhr进行ajax的封装.也可以使用基于promise封装的请求 ...
- R︱高效数据操作——data.table包(实战心得、dplyr对比、key灵活用法、数据合并)
每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- 由于业务中接触的数据量很大,于是不得不转战开始 ...
- 批量插入或更新操作之ON DUPLICATE KEY UPDATE用法
实际的开发过程中,可能会遇到这样的需求,先判断某一记录是否存在,如果不存在,添加记录,如果存在,则修改数据.在INSERT语句末尾指定ON DUPLICATE KEY UPDATE可以解决这类问题. ...
- $key 的用法
<?php $attr=array("a","b","c","d"); //$key,默认是主键值,$value, ...
- ON DUPLICATE KEY UPDATE用法
INSERT INTO `books ` (`name`,`count`,`num`) VALUES ('windows','1','2'),('','linux','1','3') ON DUPLI ...
- MySQL: ON DUPLICATE KEY UPDATE 用法 避免重复插入数据
INSERT INTO osc_visit_stats(stat_date,type,id,view_count) VALUES (?,?,?,?) ON DUPLICATEKEY UPDATE vi ...
- 通过回调函数的理解来进一步理解ajax及其注意的用法
一,再一次理解回调函数 (function($){ $.fn.shadow = function(opts){ //定义的默认的参数 var defaults = { copies: 5, opaci ...
随机推荐
- 网页插件学javascript还是jquery好啊?
文章的起因,也是在群内交流是回答一个小伙的问题,一扯就停不下来,但由于个人知识面覆盖有限,自身基础又不够扎实,仅供参考: 问这个问题之前,我个人建议先搞清什么是jquery,什么是js? jq ...
- Python 学习小结
python 学习小结 python 简明教程 1.python 文件 #!/etc/bin/python #coding=utf-8 2.main()函数 if __name__ == '__mai ...
- Python购物车程序
1.要求用户输入工资,然后打印购物菜单 2.用户可以不断的购买商品,直到钱不够为止 3.退出时格式化打印用户已购买的商品和剩余金额 salary = int(input("请输入你的工资:& ...
- V4L2框架分析学习一
转载于http://www.techbulo.com/1193.html 1.概述 Video4Linux2是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口.凡 ...
- java基础知识(五)java类
类是java的核心和本质,是java语言的基础. 一.java中的类 1.类class 一个类可以包含局部变量.实例变量.类变量(静态变量) 一个类至少有一个构造方法,如果没有,系统会默认给出一个无参 ...
- JSON数据的使用
JSON (JavaScript Object Notation)一种简单的数据格式,比xml更轻巧. JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON ...
- 关于a标签自身的click事件所带来的一些影响
众所周知a标签自身带有点击事件<a href="#"></a>从它本身的特性来讲并没有什么不好的影响,但是如果你在a标签里又加入onclick事件则< ...
- Erlang C1500K长连接推送服务-内存
上篇 Erlang C1500K长连接推送服务-性能 提到:150w连接,使用了23GB内存,每个连接占用15KB,约一半是内核使用. 大概分析一下: 1. Erlang 节点 12GB,内部因为有内 ...
- android下giflib
源码路径: android/external/giflib 用到的该lib的APP: ./external/chromium_org/android_webview/build/aosp_manife ...
- 412. Fizz Buzz
https://leetcode.com/problems/fizz-buzz/ 没什么好说的,上一个小学生解法 class Solution(object): def fizzBuzz(self, ...