【高并发】09 线程安全-并发容器J.U.C

一、线程安全并发容器JUC
1、CopyOnWriteArrayList

package com.current.flame.current;

import com.current.flame.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.*;

/**
 * @author haoxiansheng
 */
@Slf4j
@ThreadSafe
public class CopyOnWriteArrayListExample {
    // 请求总数
    public static int clientTotal = 5000;

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

    private static List<Integer> list = new CopyOnWriteArrayList<>();

    public static void main(String[] args) throws InterruptedException {
        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();
                    updateCopyOnWriteArrayLis(count);
                    semaphore.release();
                } catch (Exception e) {
                    log.info("e=>{}", e.getMessage());
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("list.size=>{}", list.size());
    }

    private static void updateCopyOnWriteArrayLis(int count ) {
        list.add(count);
    }
}

  1. 缺点:读多写少、性能消耗、不能实时性的需求
  1. 优点:读写分离、最终一致性、开辟新的空间解决线程冲突
  1. 源码
public boolean add(E e) {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        Object[] elements = getArray();
        int len = elements.length;
        Object[] newElements = Arrays.copyOf(elements, len + 1);
        newElements[len] = e;
        setArray(newElements);
        return true;
    } finally {
        lock.unlock();
    }
}


2、ConcurrentSkipListSet

package com.current.flame.current;

import com.current.flame.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;
import java.util.concurrent.*;

/**
 * @author haoxiansheng
 * 单个remove add 操作是原子性的 removeAll addAll底层调对应的remove add 所以可能被其他线程影响
 */
@Slf4j
@ThreadSafe
public class ConcurrentSkipListSetExample {
    // 请求总数
    public static int clientTotal = 5000;

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

    private static Set<Integer> set = new ConcurrentSkipListSet<>();

    public static void main(String[] args) throws InterruptedException {
        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();
                    updateConcurrentSkipListSet(count);
                    semaphore.release();
                } catch (Exception e) {
                    log.info("e=>{}", e.getMessage());
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("set.size=>{}", set.size());
    }

    private static void updateConcurrentSkipListSet(int count ) {
        set.add(count);
    }
}


3、CopyOnWriteArraySet

package com.current.flame.current;

import com.current.flame.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Set;
import java.util.concurrent.*;

/**
 * @author haoxiansheng
 */
@Slf4j
@ThreadSafe
public class CopyOnWriteArraySetExample {
    // 请求总数
    public static int clientTotal = 5000;

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

    private static Set<Integer> set = new CopyOnWriteArraySet<>();

    public static void main(String[] args) throws InterruptedException {
        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();
                    updateCopyOnWriteArraySet(count);
                    semaphore.release();
                } catch (Exception e) {
                    log.info("e=>{}", e.getMessage());
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("set.size=>{}", set.size());
    }

    private static void updateCopyOnWriteArraySet(int count ) {
        set.add(count);
    }
}

4、ConcurrentHashMap

package com.current.flame.current;

import com.current.flame.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.*;

/**
 * @author haoxiansheng
 */
@Slf4j
@ThreadSafe
public class ConcurrentHashMapExample {
    // 请求总数
    public static int clientTotal = 5000;

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

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

    public static void main(String[] args) throws InterruptedException {
        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();
                    updateConcurrentHashMap(count);
                    semaphore.release();
                } catch (Exception e) {
                    log.info("e=>{}", e.getMessage());
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("map.size=>{}", map.size());
    }

    private static void updateConcurrentHashMap(int count ) {
        map.put(count, count);
    }
}

5、ConcurrentSkipListMap

package com.current.flame.current;

import com.current.flame.annoations.ThreadSafe;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.*;

/**
 * @author haoxiansheng
 */
@Slf4j
@ThreadSafe
public class ConcurrentSkipListMapExample {
    // 请求总数
    public static int clientTotal = 5000;

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

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

    public static void main(String[] args) throws InterruptedException {
        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();
                    updateConcurrentSkipListMap(count);
                    semaphore.release();
                } catch (Exception e) {
                    log.info("e=>{}", e.getMessage());
                }
                countDownLatch.countDown();
            });
        }
        countDownLatch.await();
        executorService.shutdown();
        log.info("map.size=>{}", map.size());
    }

    private static void updateConcurrentSkipListMap(int count ) {
        map.put(count, count);
    }
}

6、ConcurrentSkipListMap是有序的、支持更高的并发

二、安全共享对象策略

1、线程限制:一个被线程限制的对象,由线程独占,并且只能被占有它的线程修改

2、共享只读:一个共享只读的对象,在没有额外同步的情况下,可以被多个线程并发访问,但是任何线程都不能修改它

3、线程安全对象: 一个线程安全的对象或者容器,在内部通过同步机制来保证线程安全,所以其他线程无需额外的同步就可以通过公共接口随意访问它。

4、被守护对象: 被守护对象只能通过获取特点的锁来访问

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值