并发测试工具总结

参考

模拟并发的 4 种方法

CountDownLatch 并发测试类

@Slf4j
public class ConcurrentTest {

	/**
     * 多线程并发数
     */
    private static final int COUNT = 2000;

    private static final Set<String> HASHSET = new ConcurrentHashSet<>();

    private static final AtomicInteger REPEAT_TIMES = new AtomicInteger(0);

    public static void main(String[] args) {
        test();
    }

    /**
     * 多线程并发测试
     */
    public static void test() {
        final CountDownLatch latch = new CountDownLatch(COUNT);

        for (int i = 0; i < COUNT; i++) {
            GlobalThreadPool.execute(() -> {
                try {
                    biz();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                latch.countDown();
            });

            if (i == COUNT - 1) {
                log.info("重复次数={}", REPEAT_TIMES);
            }
        }

        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 业务方法
     */
    public static void biz() {
        String str = System.nanoTime() + "";
        log.info("{}", str);

        boolean add = HASHSET.add(str);
        if (!add) {
            log.info("重复:{}", str);
            REPEAT_TIMES.getAndIncrement();
        }
    }
}

自定义线程池并发测试


import cn.hutool.core.collection.ConcurrentHashSet;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class ConcurrentTest {

    /**
     * 多线程并发数
     */
    private static final int COUNT = 10000;

    private static final Set<String> HASHSET = new ConcurrentHashSet<>();

    private static final AtomicInteger REPEAT_TIMES = new AtomicInteger(0);

    public static void main(String[] args) throws InterruptedException {
        //IdHelper.nextId();
        test();
    }

    /**
     * 自定义线程池
     */
    private static final ThreadPoolExecutor testThreadPool = new ThreadPoolExecutor(
            100,
            1000,
            30L,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(10000),
            r -> new Thread(r, "testThreadPool-" + r.hashCode()),
            (r, executor) -> {
                r.run();
                log.warn(">>>>>>>>>>> test,task too fast, match threadpool rejected handler(run now).");
            });

    /**
     * 多线程并发测试
     */
    public static void test() throws InterruptedException {
        //final CountDownLatch latch = new CountDownLatch(COUNT);

        for (int i = 0; i < COUNT; i++) {
            testThreadPool.execute(() -> {
                try {
                    biz();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //latch.countDown();
            });

            if (i == COUNT - 1) {
                log.info("重复次数={}", REPEAT_TIMES);
            }
        }

        //latch.await();

    }

    /**
     * 业务方法
     */
    public static void biz() {
        String id = IdHelper.nextId() + "";
        //log.info("{}", id);

        boolean add = HASHSET.add(id);
        if (!add) {
            log.info("重复:{}", id);
            REPEAT_TIMES.getAndIncrement();
        }
    }
}


版权声明:本文为fzy629442466原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。