线程池

线程池

1.线程池有多套生成策略

利用hutool的builder抽象,可以通过构造模式生成一个符合自己业务需求的线程池

2.线程池的创建

ThreadPoolExecutor threadPool = ExecutorBuilder.create()
                .setCorePoolSize(10)
                .setMaxPoolSize(20)
                .setWorkQueue(new LinkedBlockingDeque<>(100))
                .build();
  • 核心线程10
  • 最大处理线程20
  • 有界队列100

3.ExecutorBuilder谁来提供?

package cn.hutool.core.thread 来提供

原生线程池

private ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2, new ThreadFactory() {
        String namePrefix = "XbpListener-Worker-";
        private final AtomicInteger nextId = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable task) {
            String name = namePrefix + nextId.getAndIncrement();
            Thread thread = new Thread(task, name);
            return thread;
        }
    });
posted @ 2023-08-21 14:35  SpecialSpeculator  阅读(34)  评论(0编辑  收藏  举报