并发容器 J.U.C ConcurrentHashMap & ConcurrentSkipListMap

和线程不安全容器的对应关系

  • HashMap -> ConcurrentHashMap
  • TreeMap -> ConcurrentSkipListMap

ConcurrentHashMap概述

  • 不允许空值;
  • 针对读做了大量的优化,具有非常高的并发性;
  • 关于HashMap和ConcurrentHashMap的详细介绍和比较,见:HashMap与ConcurrentHashMap解析与比较

ConcurrentHashMap示例

import com.example.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
@ThreadSafe
public class ConcurrentHashMapExample {

    // 请求总数
    public static int clientTotal = 5000;

    // 同时并发执行的线程数
    public static int threadTotal = 200;

    private static Map map = new ConcurrentHashMap<>();

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
            final int count = i;
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    update(count);
                    semaphore.release();
                } catch (Exception e) {
                    log.error("exception", e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("size:{}", map.size());
    }

    private static void update(int i) {
        map.put(i, i);
    }
}

输出:

14:26:49.264 [main] INFO com.example.concurrency.example.concurrent.ConcurrentHashMapExample - size:5000

ConcurrentSkipListMap概述

  • 内部使用SkipList(跳表)实现;
相较于ConcurrentHashMap的优点
  • key有序,ConcurrentHashMap做不到;
  • 支持更高的并发,其存取时间和线程数几乎没有关系,即数据量不变的情况下,并发的线程越多,ConcurrentSkipListMap的优势越明显;

ConcurrentSkipListMap示例

import com.example.concurrency.annotations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

@Slf4j
@ThreadSafe
public class ConcurrentSkipListMapExample {

    // 请求总数
    public static int clientTotal = 5000;

    // 同时并发执行的线程数
    public static int threadTotal = 200;

    private static Map map = new ConcurrentSkipListMap<>();

    public static void main(String[] args) throws Exception {
        ExecutorService executorService = Executors.newCachedThreadPool();
        final Semaphore semaphore = new Semaphore(threadTotal);
        final CountDownLatch countDownLatch = new CountDownLatch(clientTotal);
        for (int i = 0; i < clientTotal; i++) {
            final int count = i;
            executorService.execute(() -> {
                try {
                    semaphore.acquire();
                    update(count);
                    semaphore.release();
                } catch (Exception e) {
                    log.error("exception", e);
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("size:{}", map.size());
    }

    private static void update(int i) {
        map.put(i, i);
    }
}

输出:

14:27:42.507 [main] INFO com.example.concurrency.example.concurrent.ConcurrentSkipListMapExample - size:5000

你可能感兴趣的:(并发容器 J.U.C ConcurrentHashMap & ConcurrentSkipListMap)