深入理解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 ...
随机推荐
- runtime-对成员变量操作应用之归档和返归档
为了实现归档和返归档,我们要让被归档对象的类接受NSCoding协议并且实现协议里的两个方法 - (void)encodeWithCoder:(NSCoder *)aCoder; - (nullabl ...
- SQL Server基线算法(同比和环比)
基线介绍 基线为历史数据统计而成的数据,具有参考价值,并利用基线数据与当前值的对比,通过一定的报警机制,形成实时监控架构.SQL Server计数器采用同比和环比两种方式. 同比:可以计算未来一周的基 ...
- 设备旋转,创建水平模式布局--Android studio
1.在项目工具窗口中,右键单击res目录后选择new--Android resource directory菜单项. 2.从资源类型Resource type列表中选择layout,保持Source ...
- Ubuntu虚拟机中断后重启网络断接错误解决方案
因为该死的windows自动更新,所以vmplayer经常会被强制关闭. 但重新启动后,会发生不能连接到网络的情况显示: waiting for the network configuration…… ...
- VB编程的键盘控制
在VB中,很多控件都提供了键盘输入功能(如文本框.组合框等),但它们对键盘的处理较简单,遇到复杂一点的要求即无能为力. 因而VB提供了KeyPress.KeyUp.KeyDown三种键盘事件,当用户按 ...
- MySQL 应用优化
一.使用连接池 二.减少对MySQL的访问 (A) 避免对同一数据做重复检索. (B) 使用查询缓存,MySQL的查询缓存会存储SELECT查询的命令文本和相应的结果. (C) 增加CACHE层 三. ...
- 【openStack】Libcloud 如何支持 keystone V3?
Examples This section includes some examples which show how to use the newly available functionality ...
- Python 之旅
Python2 之旅: https://funhacks.net/explore-python/ <Python Cookbook>第三版 PYTHON3 http://pyt ...
- selenium 3.0 beta2 初体验
经过漫长的等待,终于迎来了selenium 3.0 从selenium 1.0 至今,差不多有十多年的历史.这个月终于迎来了selenium3.0 那么selenium3.0 为我们带来了什么? 看一 ...
- php二进制安全的含义
PHP里,有string的概念.string里,每个字符的大小为byte(与PHP相比,Java的每个字符为Character,是UTF8字符,C语言的每个字符可以在编译时选择). byte里,有AS ...