ConcurrentHashMap、synchronized与线程安全

来源:互联网 发布:淘宝招聘在家工作 编辑:程序博客网 时间:2024/06/06 08:29

最近做的项目中遇到一个问题:明明用了ConcurrentHashMap,可是始终线程不安全

除去项目中的业务逻辑,简化后的代码如下:

[java] view plain copy
  1. public class Test40 {  
  2.   
  3.     public static void main(String[] args) throws InterruptedException {  
  4.         for (int i = 0; i < 10; i++) {  
  5.             System.out.println(test());  
  6.         }  
  7.     }  
  8.       
  9.     private static int test() throws InterruptedException {  
  10.         ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();  
  11.         ExecutorService pool = Executors.newCachedThreadPool();  
  12.         for (int i = 0; i < 8; i++) {  
  13.             pool.execute(new MyTask(map));  
  14.         }  
  15.         pool.shutdown();  
  16.         pool.awaitTermination(1, TimeUnit.DAYS);  
  17.           
  18.         return map.get(MyTask.KEY);  
  19.     }  
  20. }  
  21.   
  22. class MyTask implements Runnable {  
  23.       
  24.     public static final String KEY = "key";  
  25.       
  26.     private ConcurrentHashMap<String, Integer> map;  
  27.       
  28.     public MyTask(ConcurrentHashMap<String, Integer> map) {  
  29.         this.map = map;  
  30.     }  
  31.   
  32.     @Override  
  33.     public void run() {  
  34.         for (int i = 0; i < 100; i++) {  
  35.             this.addup();  
  36.         }  
  37.     }  
  38.       
  39.     private void addup() {  
  40.         if (!map.containsKey(KEY)) {  
  41.             map.put(KEY, 1);  
  42.         } else {  
  43.             map.put(KEY, map.get(KEY) + 1);  
  44.         }      
  45.     }  
  46. }  

测试代码跑了10次,每次都不是800。这就很让人疑惑了,难道ConcurrentHashMap的线程安全性失效了?

查了一些资料后发现,原来ConcurrentHashMap的线程安全指的是,它的每个方法单独调用(即原子操作)都是线程安全的,但是代码总体的互斥性并不受控制。以上面的代码为例,最后一行中的

[java] view plain copy
  1. map.put(KEY, map.get(KEY) + 1);  

实际上并不是原子操作,它包含了三步:

  1. map.get
  2. 加1
  3. map.put

其中第1和第3步,单独来说都是线程安全的,由ConcurrentHashMap保证。但是由于在上面的代码中,map本身是一个共享变量。当线程A执行map.get的时候,其它线程可能正在执行map.put,这样一来当线程A执行到map.put的时候,线程A的值就已经是脏数据了,然后脏数据覆盖了真值,导致线程不安全

简单地说,ConcurrentHashMap的get方法获取到的是此时的真值,但它并不保证当你调用put方法的时候,当时获取到的值仍然是真值

为了使上面的代码变得线程安全,我引入了synchronized关键字来修饰目标方法,如下:

[java] view plain copy
  1. public class Test40 {  
  2.   
  3.     public static void main(String[] args) throws InterruptedException {  
  4.         for (int i = 0; i < 10; i++) {  
  5.             System.out.println(test());  
  6.         }  
  7.     }  
  8.       
  9.     private static int test() throws InterruptedException {  
  10.         ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();  
  11.         ExecutorService pool = Executors.newCachedThreadPool();  
  12.         for (int i = 0; i < 8; i++) {  
  13.             pool.execute(new MyTask(map));  
  14.         }  
  15.         pool.shutdown();  
  16.         pool.awaitTermination(1, TimeUnit.DAYS);  
  17.           
  18.         return map.get(MyTask.KEY);  
  19.     }  
  20. }  
  21.   
  22. class MyTask implements Runnable {  
  23.       
  24.     public static final String KEY = "key";  
  25.       
  26.     private ConcurrentHashMap<String, Integer> map;  
  27.       
  28.     public MyTask(ConcurrentHashMap<String, Integer> map) {  
  29.         this.map = map;  
  30.     }  
  31.   
  32.     @Override  
  33.     public void run() {  
  34.         for (int i = 0; i < 100; i++) {  
  35.             this.addup();  
  36.         }  
  37.     }  
  38.       
  39.     private synchronized void addup() { // 用关键字synchronized修饰addup方法  
  40.         if (!map.containsKey(KEY)) {  
  41.             map.put(KEY, 1);  
  42.         } else {  
  43.             map.put(KEY, map.get(KEY) + 1);  
  44.         }  
  45.     }  
  46.       
  47. }  

运行之后仍然是线程不安全的,难道synchronized也失效了?

查阅了synchronized的资料后,原来,不管synchronized是用来修饰方法,还是修饰代码块,其本质都是锁定某一个对象。修饰方法时,锁上的是调用这个方法的对象,即this;修饰代码块时,锁上的是括号里的那个对象

在上面的代码中,很明显就是锁定的MyTask对象本身。但是由于在每一个线程中,MyTask对象都是独立的,这就导致实际上每个线程都对自己的MyTask进行锁定,而并不会干涉其它线程的MyTask对象。换言之,上锁压根没有意义

理解到这点之后,对上面的代码又做了一次修改:

[java] view plain copy
  1. public class Test40 {  
  2.   
  3.     public static void main(String[] args) throws InterruptedException {  
  4.         for (int i = 0; i < 10; i++) {  
  5.             System.out.println(test());  
  6.         }  
  7.     }  
  8.       
  9.     private static int test() throws InterruptedException {  
  10.         ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String, Integer>();  
  11.         ExecutorService pool = Executors.newCachedThreadPool();  
  12.         for (int i = 0; i < 8; i++) {  
  13.             pool.execute(new MyTask(map));  
  14.         }  
  15.         pool.shutdown();  
  16.         pool.awaitTermination(1, TimeUnit.DAYS);  
  17.           
  18.         return map.get(MyTask.KEY);  
  19.     }  
  20. }  
  21.   
  22. class MyTask implements Runnable {  
  23.       
  24.     public static final String KEY = "key";  
  25.       
  26.     private ConcurrentHashMap<String, Integer> map;  
  27.       
  28.     public MyTask(ConcurrentHashMap<String, Integer> map) {  
  29.         this.map = map;  
  30.     }  
  31.   
  32.     @Override  
  33.     public void run() {  
  34.         for (int i = 0; i < 100; i++) {  
  35.             synchronized (map) { // 对共享对象map上锁  
  36.                 this.addup();  
  37.             }  
  38.         }  
  39.     }  
  40.       
  41.     private void addup() {  
  42.         if (!map.containsKey(KEY)) {  
  43.             map.put(KEY, 1);  
  44.         } else {  
  45.             map.put(KEY, map.get(KEY) + 1);  
  46.         }  
  47.     }  
  48.       
  49. }  

此时在调用addup时直接锁定map,由于map是被所有线程共享的,因而达到了让所有线程互斥的目的,线程安全达成。

注意:

synchronized {普通方法}依靠对象锁工作,多线程访问synchronized方法,一旦某个进程抢得锁之后,其他的进程只有排队对待。synchronized void method{}功能上,等效于
void method{
   synchronized(this) {
    ...
   }
}
2.synchronized {static方法}此代码块等效于
void method{
   synchronized(Obl.class)
   }
}
使用该类的类对象的锁定去做线程的共享互斥.
在spring源码中的使用:DefaultListableBeanFactory.java  registerBeanDefinition

  1. /** Map of bean definition objects, keyed by bean name */  
  2.   
  3.     private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>(64);  
  4.   
  5. //---------------------------------------------------------------------  
  6.     // Implementation of BeanDefinitionRegistry interface  
  7.     //---------------------------------------------------------------------  
  8.   
  9.     public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)  
  10.             throws BeanDefinitionStoreException {  
  11.   
  12.         Assert.hasText(beanName, "Bean name must not be empty");  
  13.         Assert.notNull(beanDefinition, "BeanDefinition must not be null");  
  14.   
  15.         if (beanDefinition instanceof AbstractBeanDefinition) {  
  16.             try {  
  17.                 ((AbstractBeanDefinition) beanDefinition).validate();  
  18.             }  
  19.             catch (BeanDefinitionValidationException ex) {  
  20.                 throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,  
  21.                         "Validation of bean definition failed", ex);  
  22.             }  
  23.         }  
  24.   
  25.         BeanDefinition oldBeanDefinition;  
  26.   
  27.         synchronized (this.beanDefinitionMap) {  
  28.             oldBeanDefinition = this.beanDefinitionMap.get(beanName);  
  29.             if (oldBeanDefinition != null) {  
  30.                 if (!this.allowBeanDefinitionOverriding) {  
  31.                     throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(),beanName,  
  32.                             "Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +  
  33.                             "': There is already [" + oldBeanDefinition + "] bound.");  
  34.                 }  
  35.                 else {  
  36.                     if (this.logger.isInfoEnabled()) {  
  37.                         this.logger.info("Overriding bean definition for bean '" + beanName +  
  38.                                 "': replacing [" + oldBeanDefinition + "] with [" + beanDefinition + "]");  
  39.                     }  
  40.                 }  
  41.             }  
  42.             else {  
  43.                 this.beanDefinitionNames.add(beanName);  
  44.                 this.frozenBeanDefinitionNames = null;  
  45.             }  
  46.             this.beanDefinitionMap.put(beanName, beanDefinition);  
  47.         }  
  48.   
  49.         if (oldBeanDefinition != null || containsSingleton(beanName)) {  
  50.             resetBeanDefinition(beanName);  
  51.         }  
  52.     }  


原创粉丝点击