#
vincentlu
2025-05-13 ebd2f4397a92c6a5096de1b86d59154363344720
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.zy.acs.gateway;
 
import com.google.common.util.concurrent.ThreadFactoryBuilder;
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;
import java.util.concurrent.atomic.AtomicInteger;
 
/**
 * 总线程
 * Created by vincent on 2019-04-03
 */
@Component("executors")
public class Executors implements DisposableBean {
 
    private static final AtomicInteger count = new AtomicInteger();
 
    private ExecutorService executorService;
 
    private static final int cpus = Runtime.getRuntime().availableProcessors();
 
 
    /**
     * 业务线程池
     */
    public ExecutorService getInstance() {
        if (null == executorService){
            executorService = new ThreadPoolExecutor(cpus, cpus << 3,
                    30 , TimeUnit.SECONDS,
                    new LinkedBlockingQueue<>(512),
                    new ThreadFactoryBuilder().setNameFormat("task-pool").build(),
                    new ThreadPoolExecutor.CallerRunsPolicy());
        }
 
        return executorService;
 
    }
 
    /**
     * 监听器线程池
     *
     */
    public static ExecutorService getCache() {
        return java.util.concurrent.Executors.newFixedThreadPool(cpus >> 1, r -> {
            Thread thread = new Thread(r);
            thread.setName("zy-acs-task-factory" + count.getAndIncrement());
            if (thread.isDaemon()) {
                thread.setDaemon(Boolean.FALSE);
            }
            thread.setPriority(Thread.NORM_PRIORITY);
 
            return thread;
        });
 
    }
 
 
    @Override
    public void destroy() {
        if (executorService != null && !executorService.isShutdown()) {
            executorService.shutdown();
        }
    }
 
}