package com.elab.core.componts;

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

/**
 * @Module 工具类
 * @Description 并发工具类
 * @Author liukaixiong
 * @Date 2021/1/5 11:08
 */
public class ConcurrentTool {

    public int count;

    private CyclicBarrier cyclicBarrier;

    public ConcurrentTool(int count) {
        this.count = count;
        this.cyclicBarrier = new CyclicBarrier(count);
    }

    /**
     * 执行压测方式
     *
     * @param supplier
     */
    public void process(Supplier supplier) {
        for (int i = 0; i < count; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                        Object o = supplier.get();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
