HashMap、ConcurrentHashMap和SynchronizedMap – 哈希表在Java中的同步处理

原文链接:http://crunchify.com/hashmap-vs-concurrenthashmap-vs-synchronizedmap-how-a-hashmap-can-be-synchronized-in-java/

在Java中,HashMap是一个非常有用的数据结构。几乎每一个Java应用都会使用到它。我之前的博文中有介绍过如何实现一个线程安全的缓存,在这个例子中,我就使用到了HashMap。然而,需要注意的是,HashMap本身并不是一个线程安全的Collection类

常见问题

  • ConcurrentHashMapCollections.synchronizedMap(Map)分别是什么?
  • ConcurrentHashMapCollections.synchronizedMap(Map)在性能上有什么区别?
  • ConcurrentHashMapCollections.synchronizedMap(Map)的优劣对比?
  • 关于HashMapConcurrentHashMap的常见面试问题

在这篇文章中,将涵盖以上的问题,并解释我们应该如何将HashMap变得线程安全。

原因

Map是一个通过键值对的形势来存储元素的容器,其中,要求key保持唯一,并且每一个key唯一对应一个value。如果你在设计一个高度并行化的程序,并且需要在不同的线程中去读取或者修改一个Map对象,那么使用线程安全的Map则是一个理想的选择。最典型的一个例子就是生产者-消费者模式,生产者不断的修改Map而消费者同时也在读取Map中的值。

那么对于Map来说,什么是线程安全呢?简单的来说,就是当多个线程同时在使用一个Map的时候,至少有一个线程对Map的结构进行了修改,那么必须保证这个修改被立即同步到其他线程中去,避免其他线程获取到错误的值。

解决方案

有两种方法可以解决HashMap的线程安全问题:

  • Java的Collections库中的synchronizedMap()方法
  • 使用ConcurrentHashMap

译者注:其实还有第三种方法,使用Hashtable。不过Hashtable是Java 1.1提供的旧有类,从性能上和使用上都不如其他的替代类,因此已经不推荐使用

//Hashtable
Map<String, String> normalMap = new Hashtable<String, String>();

//synchronizedMap
synchronizedHashMap = Collections.synchronizedMap(new HashMap<String, String>());

//ConcurrentHashMap
concurrentHashMap = new ConcurrentHashMap<String, String>();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

ConcurrentHashMap

  • 当你程序需要高度的并行化的时候,你应该使用ConcurrentHashMap
  • 尽管没有同步整个Map,但是它仍然是线程安全的
  • 读操作非常快,而写操作则是通过加锁完成的
  • 在对象层次上不存在锁(即不会阻塞线程)
  • 锁的粒度设置的非常好,只对哈希表的某一个key加锁
  • ConcurrentHashMap不会抛出ConcurrentModificationException,即使一个线程在遍历的同时,另一个线程尝试进行修改。
  • ConcurrentHashMap会使用多个锁

SynchronizedHashMap

  • 会同步整个对象
  • 每一次的读写操作都需要加锁
  • 对整个对象加锁会极大降低性能
  • 这相当于只允许同一时间内至多一个线程操作整个Map,而其他线程必须等待
  • 它有可能造成资源冲突(某些线程等待较长时间)
  • SynchronizedHashMap会返回Iterator,当遍历时进行修改会抛出异常

示例

  1. 创建类CrunchifyConcurrentHashMapVsSynchronizedHashMap.java
  2. 实例化每一个对象(HashTableSynchronizedMap 和 ConcurrentHashMap
  3. 添加/读取5*50万行数据到Map中
  4. 测试开始和结束的时间
  5. 使用ExecutorService来开启5个线程,同时读写
package crunchify.com.tutorials;

import java.util.Collections;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author Crunchify.com
 *
 */

public class CrunchifyConcurrentHashMapVsSynchronizedMap {

    public final static int THREAD_POOL_SIZE = 5;

    public static Map<String, Integer> crunchifyHashTableObject = null;
    public static Map<String, Integer> crunchifySynchronizedMapObject = null;
    public static Map<String, Integer> crunchifyConcurrentHashMapObject = null;

    public static void main(String[] args) throws InterruptedException {

        // Test with Hashtable Object
        crunchifyHashTableObject = new Hashtable<String, Integer>();
        crunchifyPerformTest(crunchifyHashTableObject);

        // Test with synchronizedMap Object
        crunchifySynchronizedMapObject = Collections.synchronizedMap(new HashMap<String, Integer>());
        crunchifyPerformTest(crunchifySynchronizedMapObject);

        // Test with ConcurrentHashMap Object
        crunchifyConcurrentHashMapObject = new ConcurrentHashMap<String, Integer>();
        crunchifyPerformTest(crunchifyConcurrentHashMapObject);

    }

    public static void crunchifyPerformTest(final Map<String, Integer> crunchifyThreads) throws InterruptedException {

        System.out.println("Test started for: " + crunchifyThreads.getClass());
        long averageTime = 0;
        for (int i = 0; i < 5; i++) {

            long startTime = System.nanoTime();
            ExecutorService crunchifyExServer = Executors.newFixedThreadPool(THREAD_POOL_SIZE);

            for (int j = 0; j < THREAD_POOL_SIZE; j++) {
                crunchifyExServer.execute(new Runnable() {
                    @SuppressWarnings("unused")
                    @Override
                    public void run() {

                        for (int i = 0; i < 500000; i++) {
                            Integer crunchifyRandomNumber = (int) Math.ceil(Math.random() * 550000);

                            // Retrieve value. We are not using it anywhere
                            Integer crunchifyValue = crunchifyThreads.get(String.valueOf(crunchifyRandomNumber));

                            // Put value
                            crunchifyThreads.put(String.valueOf(crunchifyRandomNumber), crunchifyRandomNumber);
                        }
                    }
                });
            }

            // Make sure executor stops
            crunchifyExServer.shutdown();

            // Blocks until all tasks have completed execution after a shutdown request
            crunchifyExServer.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS);

            long entTime = System.nanoTime();
            long totalTime = (entTime - startTime) / 1000000L;
            averageTime += totalTime;
            System.out.println("2500K entried added/retrieved in " + totalTime + " ms");
        }
        System.out.println("For " + crunchifyThreads.getClass() + " the average time is " + averageTime / 5 + " ms\n");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

结果如下:

Test started for: class java.util.Hashtable
500K entried added/retrieved in 1432 ms
500K entried added/retrieved in 1425 ms
500K entried added/retrieved in 1373 ms
500K entried added/retrieved in 1369 ms
500K entried added/retrieved in 1438 ms
For class java.util.Hashtable the average time 1407 ms

Test started for: class java.util.Collections$SynchronizedMap
500K entried added/retrieved in 1431 ms
500K entried added/retrieved in 1460 ms
500K entried added/retrieved in 1387 ms
500K entried added/retrieved in 1456 ms
500K entried added/retrieved in 1406 ms
For class java.util.Collections$SynchronizedMap the average time 1428 ms

Test started for: class java.util.concurrent.ConcurrentHashMap
500K entried added/retrieved in 413 ms
500K entried added/retrieved in 351 ms
500K entried added/retrieved in 427 ms
500K entried added/retrieved in 337 ms
500K entried added/retrieved in 339 ms
For class java.util.concurrent.ConcurrentHashMap the average time 373 ms  <== Much faster

猜你喜欢

转载自blog.csdn.net/weixin_39935887/article/details/80656465