package com.jay.monitor.data.server.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CyclicBarrier;
import java.util.function.Supplier;

/**
 * @Module TODO
 * @Description TODO
 * @Author liukaixiong
 * @Date 2020/11/9 11:04
 */
public class ThreadUtils {

    private static Logger logger = LoggerFactory.getLogger(ThreadUtils.class);

    /**
     * 压测工具类
     *
     * @param number       压测次数
     * @param count        压测数量
     * @param intervalTime 每个压测之间的间隔时间
     * @param consumer     压测的具体内容
     */
    public static void concurrent(int number, int count, int intervalTime, Supplier consumer) {
        CyclicBarrier barrier = new CyclicBarrier(count);
        for (int i = 0; i < number; i++) {
            logger.info("压测第[" + i + "]次");
            for (int j = 0; j < 10; j++) {
                new Thread(() -> {
                    try {
                        logger.info("开始等待");
                        barrier.await();
                        logger.info("抢占开始");
                        Object o = consumer.get();
                        logger.info("得到的结果:" + o);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }).start();
            }
        }
    }
}
