ConcurrentMap线程安全的正确用法

ConcurrentMap线程安全的正确用法

原创  2017年11月28日 15:35:14
[java]  view plain  copy
  1. import java.util.HashMap;  
  2. import java.util.Map;  
  3. import java.util.concurrent.ConcurrentHashMap;  
  4. import java.util.concurrent.ConcurrentMap;  
  5. import java.util.concurrent.ExecutionException;  
  6.   
  7. public class ConcurrentMapWithMap {    
  8.       
  9.     private static Map<String, Long> mapWordCounts = new HashMap<>();    
  10.     private static ConcurrentMap<String, Long> concurrentMapWordCounts = new ConcurrentHashMap<>();    
  11.     public static int count=0;    
  12.         
  13.     public static long mapIncrease(String word) {    
  14.         Long oldValue = mapWordCounts.get(word);    
  15.         Long newValue = (oldValue == null) ? 1L : oldValue + 1;    
  16.         mapWordCounts.put(word, newValue);    
  17.         return newValue;    
  18.     }    
  19.         
  20.          
  21.     public static long ConcurrentMapIncrease(String word) {    
  22.         Long oldValue, newValue;    
  23.         while (true) {    
  24.             oldValue = concurrentMapWordCounts.get(word);    
  25.             if (oldValue == null) {    
  26.                 newValue = 1L;    
  27.                 //已经有key了就返回放入的值,否则返回空  
  28.                 if (concurrentMapWordCounts.putIfAbsent(word, newValue) == null) {    
  29.                     break;    
  30.                 }    
  31.             } else {    
  32.                 newValue = oldValue + 1;    
  33.                 //值替换,每次替换时都会比较上面拿到oldValue是否就是当前map里面的值,是才替换,否则继续获取  
  34.                 if (concurrentMapWordCounts.replace(word, oldValue, newValue)) {    
  35.                     break;    
  36.                 }    
  37.             }    
  38.         }    
  39.         return newValue;  
  40.     }    
  41.         
  42.     public static void mapWordCount() throws InterruptedException, ExecutionException {    
  43.         new Thread(new Runnable(){      
  44.             public void run() {    
  45.                 int count=0;    
  46.                 while(count++<10000)   
  47.                     ConcurrentMapWithMap.mapIncrease("work");    
  48.             }    
  49.         }).start();    
  50.         new Thread(new Runnable(){      
  51.             public void run() {    
  52.                 int count=0;    
  53.                 while(count++<10000)   ;  
  54.                     ConcurrentMapWithMap.mapIncrease("work");    
  55.             }    
  56.         }).start();    
  57.         new Thread(new Runnable(){      
  58.             public void run() {    
  59.                 int count=0;    
  60.                 while(count++<10000)     
  61.                    ConcurrentMapWithMap.mapIncrease("work");    
  62.             }    
  63.         }).start();    
  64.         new Thread(new Runnable(){      
  65.             public void run() {    
  66.                 int count=0;    
  67.                 while(count++<10000)     
  68.                     ConcurrentMapWithMap.mapIncrease("work");    
  69.             }    
  70.         }).start();    
  71.     }                   
  72.             
  73.     public static void concurrentWordCount() throws InterruptedException, ExecutionException {    
  74.         new Thread(new Runnable(){      
  75.             public void run() {    
  76.                 int count=0;    
  77.                 while(count++<10000)     
  78.                     ConcurrentMapWithMap.ConcurrentMapIncrease("work");    
  79.             }    
  80.         }).start();    
  81.         new Thread(new Runnable(){      
  82.             public void run() {    
  83.                 int count=0;    
  84.                 while(count++<10000)     
  85.                     ConcurrentMapWithMap.ConcurrentMapIncrease("work");    
  86.             }    
  87.         }).start();    
  88.         new Thread(new Runnable(){      
  89.             public void run() {    
  90.                 int count=0;    
  91.                 while(count++<10000)     
  92.                     ConcurrentMapWithMap.ConcurrentMapIncrease("work");    
  93.             }    
  94.         }).start();    
  95.         new Thread(new Runnable(){      
  96.             public void run() {    
  97.                 int count=0;    
  98.                 while(count++<10000)     
  99.                     ConcurrentMapWithMap.ConcurrentMapIncrease("work");    
  100.             }    
  101.         }).start();    
  102.     }      
  103.     
  104.     public static void main(String[] args) throws InterruptedException, ExecutionException {    
  105.         ConcurrentMapWithMap.mapWordCount();    
  106.         Thread.sleep(10000);    
  107.         System.out.println("final count map"+ConcurrentMapWithMap.mapWordCounts.get("work"));//多线程累加,每次都少于40000,故线程不安全    
  108.         ConcurrentMapWithMap.concurrentWordCount();    
  109.         Thread.sleep(10000);    
  110.         System.out.println("final count concurrentMap"+ConcurrentMapWithMap.concurrentMapWordCounts.get("work"));//多线程累加,每次都是40000    
  111.     }    
  112.     
  113. }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值