欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot 生成线程池

程序员文章站 2022-05-01 13:03:53
...
@Configuration
public class ConvertThreadPoolConfig {
    private static final AtomicInteger threadIndex = new AtomicInteger(0);
    
    @Bean(value = "convertThreadPool")
    public ExecutorService convertThreadPool() {
        ThreadFactory threadFactory = new ThreadFactory() {
            
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r, "convert-thread-" + threadIndex.incrementAndGet());
                thread.setDaemon(true);
                return thread;
            }
        };
        
        ExecutorService pool = new ThreadPoolExecutor(2, 4, 0L, TimeUnit.MILLISECONDS, 
                new ArrayBlockingQueue<Runnable>(10), threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
        return pool;
    }
}