Spring boot 生成线程池

@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(10), threadFactory, new ThreadPoolExecutor.CallerRunsPolicy());
        return pool;
    }
}

你可能感兴趣的:(Spring boot 生成线程池)