#
luxiaotao1123
2024-12-25 f941a47ccd057a719eef4db7decf13bf9ad2a4f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.zy.acs.manager.core.service;
 
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.stereotype.Component;
 
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
 
/**
 * 总线程
 * Created by vincent on 2019-04-03
 */
@Slf4j
@Component
public class ThreadPoolRegulator implements DisposableBean {
 
    private static final int cpus = Runtime.getRuntime().availableProcessors();
 
    private ExecutorService executorService;
 
    public ExecutorService getInstance() {
        if (null == executorService){
            executorService = new ThreadPoolExecutor(
                    cpus,   // number of core threads
                    cpus << 3,  // max count ===>> recycle
                    5 , TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(512),
                    new ThreadFactoryBuilder().setNameFormat("Matrix-Deal-Pool").build(),
                    new ThreadPoolExecutor.CallerRunsPolicy());
        }
 
        return executorService;
    }
 
 
    @Override
    public void destroy() {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
        }
    }
 
}