并发测试工具类 封装,方便使用

2022-07-02  本文已影响0人  川流不息attitude

并发测试工具类 封装,方便使用

很多时候我们都想做一些并发测试,又不想用 JMeter ,ab等工具所以直接用代码写一个工具类

@Slf4j
public class MultiThreadConcurrentTestUtils {

    /**
     * 多线程并发测试
     * @param threadNum  线程数量
     * @param consumer 函数
     * @param param 消费参数
     * @throws InterruptedException
     */
    @SneakyThrows
    public static void multiThreadTest(int threadNum, Object param,Consumer consumer) {
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(threadNum);
        for (int i = 0; i < threadNum; i++) {
            executorService.execute(() -> {
                try {
                    Thread.sleep(new Random().nextInt(60)*1000);
                    cyclicBarrier.await();
                    // 业务方法
                    consumer.accept(param);
                    log.info("线程:{} 开始执行",Thread.currentThread().getName());
                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            });
        }
        countDownLatch.await();
        executorService.shutdown();
    }


    /**
     * 多线程测试
     * @param threadNum
     * @param runnable
     */

    @SneakyThrows
    public static void multiThreadTest(int threadNum, Runnable runnable) {
        CountDownLatch countDownLatch = new CountDownLatch(threadNum);
        ExecutorService executorService = Executors.newFixedThreadPool(threadNum);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(threadNum);
        for (int i = 0; i < threadNum; i++) {
            executorService.execute(() -> {
                try {
                    Thread.sleep(new Random().nextInt(60)*1000);
                    cyclicBarrier.await();
                    // 业务方法
                    runnable.run();
                    log.info("线程:{} 开始执行",Thread.currentThread().getName());
                    countDownLatch.countDown();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });
        }
        countDownLatch.await();
        executorService.shutdown();
    }
}

使用

  List<Integer> list = new ArrayList<>();

    List<Integer> copyList = new CopyOnWriteArrayList<>();
    @Test
    public void testDistributedLock() {
        MultiThreadConcurrentTestUtils.multiThreadTest(10,"test",(e) ->{
            for (int i = 0; i < 10; i++) {
                copyList.add(i);
            }
        });

        System.out.println("copyList "+copyList.size());
        MultiThreadConcurrentTestUtils.multiThreadTest(10,() -> {
            for (int i = 0; i < 10; i++) {
                list.add(i);
            }
        });

        System.out.println("list "+ list.size());
    }
image.png

可见 ArrayList 线程不安全,CopyOnWriteArrayList 线程安全

上一篇下一篇

猜你喜欢

热点阅读