日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

SpringBoot中線程池初始化,并且可配置線程池參數

作者:提里奧丶弗丁 更新時間: 2023-07-18 編程語言

?? ? ? ? 首先是線程池的配置類如下:

@Configuration
public class ThreadPoolHandle {

    @Value("${single.threadPool.core}")
    private Integer core;
    @Value("${single.threadPool.max}")
    private Integer max;
    @Value("${single.threadPool.keepLive}")
    private long keepLive;
    @Value("${single.threadPool.queue}")
    private Integer queue;

    @Bean
    public ThreadPoolExecutor geniusThreadPool(){
        //創建線程池
        ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
                //線程池中的常駐核心線程數(假設銀行窗口一般情況下開兩個)
                core,
                //線程池能夠容納同時執行的最大線程數,此值必須>=1(特殊情況下比如周末辦理業務人多,增設到5個窗口)
                max,
                //多余的空閑線程的存活時間 當前線程池數量超過 "corepoolsize"(認為有問題 應該是超過 maximumpoolsize吧)
                keepLive,
                // 當空閑時間達到keepalivetime值時,多余空閑線程會被銷毀直到只剩下 corePoolSize個線程為止
                //unit keepAliveTime的單位
                TimeUnit.SECONDS,
                //任務隊列,被提交但尚未被執行的任務(銀行窗口的等待區的座位)
                new LinkedBlockingQueue<>(queue),
                //線程工廠,用于生成線程池中的工作線程
                new XXXXThreadFactory("dataHandlePool"),
                //拒絕策略,當任務隊列[LindedBlockingQueue]滿了,
                // 并且工作線程[自己理解為辦理業務的總認識,如以下代碼中的10]大于等于線程池的最大線程數時如何拒絕多余工作線程
                new ThreadPoolExecutor.AbortPolicy()
        );
        threadPool.allowCoreThreadTimeOut(true);
        return threadPool;
    }

? ? ? ? yml中的配置如下:

single:
  threadPool:
    core: 8
    max: 18
    keepLive: 10
    queue: 100

? ? ? ? 線程工廠如下:

public class AtfRiskThreadFactory implements ThreadFactory {
    private static final AtomicInteger poolNumber = new AtomicInteger(1);
    private final ThreadGroup group;
    private final AtomicInteger threadNumber = new AtomicInteger(1);
    private final String namePrefix;

    AtfRiskThreadFactory(String name) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() :
                Thread.currentThread().getThreadGroup();
        if(name == null || "".equals(name.trim())){
            name = "pool";
        }
        namePrefix = name + "-" +
                poolNumber.getAndIncrement() +
                "-thread-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r,
                namePrefix + threadNumber.getAndIncrement(),
                0);
        if (t.isDaemon()){
            t.setDaemon(false);
        }
        if (t.getPriority() != Thread.NORM_PRIORITY){
            t.setPriority(Thread.NORM_PRIORITY);
        }
        return t;
    }
}

? ? ? ? 線程池的使用:

            try{

                threadPool.execute(()->{
                    
                    System.out.println(Thread.currentThread().getName()+"  "+ finalI +" 辦理業務");

                });

            }catch (RejectedExecutionException e){
                System.out.println("捕獲到異常===================->");
                System.out.println("把消息:"+finalI+"保存起來");
            }

原文鏈接:https://blog.csdn.net/m0_65014849/article/details/129534054

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新