深入理解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 ...
 
随机推荐
- asp.net core 如何在Controller获取配置文件的值
			
场景:我们会把一些配置信息,写在配置文件文件中,便于我们修改和配置.在之前的asp.net 中可以通过ConfigurationManger来获取web.config里面的配置.在.net core ...
 - Android广播接收器BroadcastRceiver
			
一.使用BroadcastRceiver 1.创建BroadcastRceiver(MyRceiver),重写OnReceiver: public void onReceive(Context con ...
 - 4种scope方法
			
默认作用域,自动加载: default_scope { order(created_at: :desc) } model 调用 find_2时才运行 scope :find_2, ->{ whe ...
 - Thrift的TBinaryProtocol二进制协议分析
			
先上张图,说明一下thrift的二进制协议是什么东东. 报文格式编码: bool类型: 一个字节的类型,两个字节的字段编号,一个字节的值(true:1,false:0). Byte类型: 一个字节的类 ...
 - 深入理解 JavaScript 变量的作用域和作用域链
			
一个变量的作用域(scope)是程序源代码中定义这个变量的区域.简单的说,作用域就是变量与函数的可访问范围.全局变量拥有全局作用域,在JavaScript代码中的任何地方都有定义.局部变量是在函数体内 ...
 - 关于JavaScript初级的知识点一(持续更新 )
			
自己刚开始接触JS这是自己一个多月以来的一些总结和回顾. 一.什么是js? js是一种弱类型的脚本语言,是HTML的3大组成部分之一.HTML标签 CSS样式 JS脚本. 二.js的5种基本数据类型 ...
 - 在_vimrc中 set noexpandtab  python 不起效果
			
我ctm,今天配置不让tab转为空格,在_vimrc中set noexpandtab 不起效果. set ts=4也不起效果. 但是在命令行中其效果. 我都不知道咋办了. 问人说我有可能使用的不是那个 ...
 - js数组中sort排序注意的地方
			
var a=[1,2,3,4,5] function sum(a,b) { return a-b } //从小到大 function obj(a,b) { return b-a } //从大到小 a. ...
 - ORA-00257: archiver error. Connect internal only, until freed.
			
早上BA抄送客户的邮件过来,说系统用不了,应用系统报异常Unable to open connection to oracle,Microsoft Provider v2.0.50727.42,既然是 ...
 - 更新Xcode后插件失效问题
			
Xcode更新后插件会失效,这个时候需要给插件的Info.plist文件添加新Xcode的UUID 一.首先找到更新后的Xcode的DVTPlugInCompatibilityUUID: 打开路径: ...