0
点赞
收藏
分享

微信扫一扫

springboot线程池使用

陆公子521 2023-05-25 阅读 63


1、配置


@Configuration
@EnableAsync
public class TaskPoolConfig {

    @Bean("taskExecutor")
    public Executor taskExecutro(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);//核心数量
        taskExecutor.setMaxPoolSize(50); //最大数量
        taskExecutor.setQueueCapacity(200);//队列长度
        taskExecutor.setKeepAliveSeconds(60);
        taskExecutor.setThreadNamePrefix("taskExecutor--");
        taskExecutor.setWaitForTasksToCompleteOnShutdown(true);
        taskExecutor.setAwaitTerminationSeconds(60);
        return taskExecutor;
    }


}

2、使用
@Component
public class AsyncTask {

    @Async("taskExecutor")
    public void tesTask(int i){
        System.out.println(Thread.currentThread().getName()+"-----"+i);
    }

}

 

举报

相关推荐

0 条评论