package com.hzs.common.core.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 * Description: 线程工具类
 * Author: jiang chao
 * Time: 2022/8/30 10:10
 * Classname: ThreadUtil
 * PackageName: com.hzs.common.core.utils
 */
@Slf4j
@Component
public class ThreadUtils {

    /**
     * 初始化创建4个长度的固定线程池
     */
    public static ExecutorService executorService = Executors.newFixedThreadPool(4);

    /**
     * 创建线程池
     */
    public static ThreadPoolExecutor threadPoolExecutor;

    static {
        // 核心线程数
        int corePoolSize = 4;
        // 最大线程数
        int maxPoolSize = 8;
        // 非核心线程的空闲时间
        long keepAliveTime = 60;
        // 队列容量
        int queueCapacity = 10;

        log.info("==========初始化线程池==========");
        // 创建线程池
        threadPoolExecutor = new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(queueCapacity)
        );
    }

}
