@Configuration
public class BeanConfig {
/** Set the ThreadPoolExecutor's core pool size. */
private int corePoolSize = 3;
/** Set the ThreadPoolExecutor's maximum pool size. */
private int maximumPoolSize = 3;
/** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */
private int queueCapacity = 5;
@Bean
public ExecutorService getExecutorService() {
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 4, TimeUnit.SECONDS,
new LinkedBlockingDeque<>(queueCapacity), new ThreadPoolExecutor.CallerRunsPolicy());
return threadPoolExecutor;
}
}
使用
@Autowired
ExecutorService executorService;
本文详细探讨了Java中如何有效地实现线程池的注入,包括在Spring框架下使用ThreadPoolTaskExecutor配置与应用,以及线程池参数的调整策略,旨在优化并发性能和系统资源利用。
799

被折叠的 条评论
为什么被折叠?



