最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ConcurrentMap.putIfAbsent(key,value)用法實(shí)例

 更新時(shí)間:2018年02月05日 11:20:20   作者:藍(lán)精靈lx  
這篇文章主要介紹了ConcurrentMap.putIfAbsent(key,value)用法實(shí)例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下

本文研究的主要是ConcurrentMap.putIfAbsent(key,value)用法的相關(guān)內(nèi)容,具體如下。

業(yè)務(wù)上經(jīng)常會遇到有這種場景,全局維護(hù)一個(gè)并發(fā)的ConcurrentMap, Map的每個(gè)Key對應(yīng)一個(gè)對象,這個(gè)對象需要只創(chuàng)建一次。如果Map中該key對應(yīng)的value不存在則創(chuàng)建,否則直接返回。

我們先看一下代碼:

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;  
  }  

這段代碼要做的事情是:

  1. 調(diào)用 map.get(key) 方法,判斷 map 里面是否有該 key 對應(yīng)的 value (Locale 對象)。
  2. 如果返回 null,表示 map 里面沒有要查找的 key-value mapping。new 一個(gè) Locale 對象,并把 new 出來的這個(gè)對象與 key 一起放入 map。
  3. 最后返回新創(chuàng)建的 Locale 對象

我們期望每次調(diào)用 getInstance 方法時(shí)要保證相同的 key 返回同一個(gè) Local 對象引用。這段代碼能實(shí)現(xiàn)這個(gè)需求嗎?

答案是:在單線程環(huán)境下可以滿足要求,但是在多線程環(huán)境下會存在線程安全性問題,即不能保證在并發(fā)的情況相同的 key 返回同一個(gè) Local 對象引用。

這是因?yàn)樵谏厦娴拇a里存在一個(gè)習(xí)慣被稱為 put-if-absent 的操作 [1],而這個(gè)操作存在一個(gè) race condition:

if (locale == null) {  
  locale = new Locale(language, country, variant);  
  map.put(key, locale);  
} 

因?yàn)樵谀硞€(gè)線程做完 locale == null 的判斷到真正向 map 里面 put 值這段時(shí)間,其他線程可能已經(jīng)往 map 做了 put 操作,這樣再做 put 操作時(shí),同一個(gè) key 對應(yīng)的 locale 對象被覆蓋掉,最終 getInstance 方法返回的同一個(gè) key 的 locale 引用就會出現(xiàn)不一致的情形。所以對 Map 的 put-if-absent 操作是不安全的(thread safty)。

為了解決這個(gè)問題,java 5.0 引入了 ConcurrentMap 接口,在這個(gè)接口里面 put-if-absent 操作以原子性方法 putIfAbsent(K key, V value) 的形式存在。正如 javadoc 寫的那樣:

putIfAbsent方法主要是在向ConcurrentHashMap中添加鍵—值對的時(shí)候,它會先判斷該鍵值對是否已經(jīng)存在。

  • 如果不存在(新的entry),那么會向map中添加該鍵值對,并返回null。
  • 如果已經(jīng)存在,那么不會覆蓋已有的值,直接返回已經(jīng)存在的值。

對上面方法進(jìn)行改造:

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 返回同一個(gè) Locale 對象引用。

這里的錯(cuò)誤出在忽視了 putIfAbsent 方法是有返回值的,并且返回值很重要。

所以,使用 putIfAbsent 方法時(shí)切記要對返回值進(jìn)行判斷。

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;  
}  

【實(shí)例1】

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class Test {
	public static void main(String[] args) {
		//測試一下currentHashMap.putIfAbsent() 
		Map<long, String> clientMap = new ConcurrentHashMap<>();
		System.out.println("首先打印空的clientMap");
		System.out.println("clientMap: " + clientMap);
		System.out.println();
		//在空的clientMap中添加一個(gè)新的記錄 
		System.out.println("在空的clientMap中添加一個(gè)新的記錄");
		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();
		//重復(fù)添加 
		System.out.println("重復(fù)添加上一次的記錄");
		System.out.println("添加之前的clientMap: " + clientMap);
		String result2 = clientMap.putIfAbsent(netId, str1);
		System.out.println("添加之后的clientMap: " + clientMap);
		System.out.println("查看返回值result: " + result2);
		System.out.println();
	}
}

總結(jié)

以上就是本文關(guān)于ConcurrentMap.putIfAbsent(key,value)用法實(shí)例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!

相關(guān)文章

  • SpringBoot集成WebSocket的兩種方式(JDK內(nèi)置版和Spring封裝版)

    SpringBoot集成WebSocket的兩種方式(JDK內(nèi)置版和Spring封裝版)

    這篇文章主要介紹了SpringBoot集成WebSocket的兩種方式,這兩種方式為JDK內(nèi)置版和Spring封裝版,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • 詳解JAVA調(diào)用WCF服務(wù)的示例代碼

    詳解JAVA調(diào)用WCF服務(wù)的示例代碼

    這篇文章主要介紹了詳解JAVA調(diào)用WCF服務(wù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-09-09
  • springboot2.x只需兩步快速整合log4j2的方法

    springboot2.x只需兩步快速整合log4j2的方法

    這篇文章主要介紹了springboot2.x只需兩步快速整合log4j2的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • 最新評論

    黑水县| 定兴县| 周口市| 桓仁| 会泽县| 富宁县| 鸡东县| 易门县| 临漳县| 永善县| 灌南县| 民丰县| 诏安县| 正安县| 通渭县| 泸溪县| 麻阳| 霞浦县| 广水市| 剑阁县| 泸水县| 黄大仙区| 土默特左旗| 武安市| 梁平县| 广汉市| 奉贤区| 定边县| 西充县| 兴和县| 南充市| 巴塘县| 长丰县| 石景山区| 皮山县| 永修县| 磴口县| 阳朔县| 瑞昌市| 策勒县| 呼伦贝尔市|