package com.qmth.themis.business.forkjoin;//package com.qmth.themis.business.threadPool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.Objects;
import java.util.concurrent.ForkJoinPool;

/**
 * @Description: 线程池应用配置
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2019/3/21
 */
@Configuration
public class ForkJoinThreadPool {
    private final static Logger log = LoggerFactory.getLogger(ForkJoinThreadPool.class);
    private ForkJoinPool forkJoinExecutor = null;
    static final int cpuNum = Runtime.getRuntime().availableProcessors();

    /**
     * 线程池
     *
     * @return
     */
    @Bean
    @Primary
    public ForkJoinPool taskForkJoinPool() {
        if (Objects.isNull(forkJoinExecutor)) {
            log.info("cpuNum:{}", cpuNum);
            forkJoinExecutor = new ForkJoinPool(cpuNum * 2, ForkJoinPool.defaultForkJoinWorkerThreadFactory, null, true);
        }
        return forkJoinExecutor;
    }

    public ForkJoinPool getForkJoinExecutor() {
        return forkJoinExecutor;
    }
}
