http://wxl24life.iteye.com/blog/1746794
先看一段代码:
- 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;
- }
- // ....
- }
在某个线程做完 locale == null 的判断到真正向 map 里面 put 值这段时间,其他线程可能已经往 map 做了 put 操作,这样再做 put 操作时,同一个 key 对应的 locale 对象被覆盖掉,最终 getInstance 方法返回的同一个 key 的 locale 引用就会出现不一致的情形。所以对 Map 的 put-if-absent 操作是不安全的(thread safty)。
故而引出了ConcurrentMap:将读与写封装为原子操作:
- 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;
- }
- // ....
- }
- locale = new Locale(language, country, variant);
- map.putIfAbsent(key, locale);
加一层判断目的有二 :
1.像单例模式那样在外做一层判断,避免putIfAbsent无谓的执行多次
2.避免海量的Locale对象的实例化
(在这段代码下,仍会存在少量Locale多次实例化、putIfAbsent多次执行,试想一下这个场景:
A线程执行null判断通过-》B线程执行null判断通过—》A线程执行putIfAbsent,将新建的对象放入-》B线程执行putIfAbsent,发现有对象了,放弃
这个场景与map的不同在于:
A线程执行null判断通过-》B线程执行null判断通过—》A线程执行put,将新建的对象放入-》B线程执行putIfAbsent,又将新建的对象放入
同样是多次执行,但ConcurrentMap不会覆盖原对象,但Map会)
但是这一段最后的返回仍有问题:(不是说这段代码的存放有问题,是返回有问题)
还回来看这个场景:
A线程执行null判断通过-》B线程执行null判断通过—》A线程执行putIfAbsent,将新建的对象放入-》B线程执行putIfAbsent,发现有对象了,放弃
“B线程执行putIfAbsent,发现有对象了,放弃”,返回者Locale并非最新存放进的对象,因为它放弃了,应当返回原值。
有两种方法:
1.
- 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 map.get(key);
- }
- // ....
- }
看最后一句:
return map.get(key);
2.利用putIfAbsent 方法的返回值
看 javadoc:
- /**
- * @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;
- }
- // ....
- }
- if (l != null) {
- locale = l;
- }
这一小段,如果putIfAbsent返回null,表示没有找到key,最终put的即是locale,返回locale即可
如果返回非null,表示在
这个并发的过程中
A线程执行null判断通过-》B线程执行null判断通过—》A线程执行putIfAbsent,将新建的对象放入-》B线程执行putIfAbsent,发现有对象了,放弃
A线程已经捷足先登,率先put,故新建的实例locale并非put进的对象,应当取putIfAbsent的返回值(即原值、即A线程抢先put的对象)
两个方法比较:
第1中方法比第2中方法多一个get操作,因此性能不如第2种方法,但是可读性更高
rongyun 的sdk单例模式就是用的第一种方法
public static RongCloud getInstance(String appKey, String appSecret) {
if (null == rongCloud.get(appKey)) {
rongCloud.putIfAbsent(appKey, new RongCloud(appKey, appSecret));
}
return rongCloud.get(appKey);
}