java 线程池的使用

1、在启动类上添加@EnableAsync // 开启多线程(线程池)

2、在service类(当然也可以不用再service类中)添加如下内容:

@Autowired
    private BussinessService bussinessService;
    @GetMapping("/test")
    public String handler(){
        System.out.println("++++++++++++++++"+Thread.currentThread().getName());
        // 模拟分页查询
        for (int i = 0; i <= 20; i++) {
            bussinessService.handler(); // 交给一个线程处理
        }
        return "成功";
    }

3、将使用多线程的类设置为@Component,且其内部多线程的处理方法上要添加@Async

@Component
public class BussinessService {
    //需要异步处理(多线程处理)
    //@Async // 使用默认的线程池配置
    @Async("taskExecutor") //指定使用那个线程池
    public void handler(){
        //获取某一页数据、批量导入到es中
        System.out.println("================"+Thread.currentThread().getName());
    }
}

如果处理的方法是这种方式的@Async(“taskExecutor”)则需要在启动类下第一个bean 内容如下(@Async中方式则不需要如下配置):

// 配置多线程  自定义线程池配置
@Bean(name = "taskExecutor")
public TaskExecutor taskExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 设置核心线程数
    executor.setCorePoolSize(20);
    // 设置最大线程数
    executor.setMaxPoolSize(40);
    // 设置队列容量
    executor.setQueueCapacity(20);
    // 设置线程活跃时间(秒)
    executor.setKeepAliveSeconds(60);
    // 设置默认线程名称
    executor.setThreadNamePrefix("taskExecutor-");
    // 设置拒绝策略
    executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    // 等待所有任务结束后再关闭线程池
    executor.setWaitForTasksToCompleteOnShutdown(true);

    executor.initialize();
    return executor;
}

启动类后进行路由测试,执行结果如下(@Async这种方式的):

++++++++++++++++http-nio-9008-exec-1
================taskExecutor-1
================taskExecutor-1
================taskExecutor-6
================taskExecutor-7
================taskExecutor-8
================taskExecutor-10
================taskExecutor-11
================taskExecutor-9
================taskExecutor-12
================taskExecutor-13
================taskExecutor-14
================taskExecutor-15
================taskExecutor-16
================taskExecutor-17
================taskExecutor-18
================taskExecutor-19
================taskExecutor-20
================taskExecutor-5
================taskExecutor-3
================taskExecutor-2
================taskExecutor-4

启动类后进行路由测试,执行结果如下(@Async(“taskExecutor”)这种方式的):

++++++++++++++++http-nio-9008-exec-1
================taskExecutor-1
================taskExecutor-1
================taskExecutor-9
================taskExecutor-10
================taskExecutor-5
================taskExecutor-12
================taskExecutor-13
================taskExecutor-14
================taskExecutor-15
================taskExecutor-16
================taskExecutor-17
================taskExecutor-20
================taskExecutor-18
================taskExecutor-19
================taskExecutor-8
================taskExecutor-7
================taskExecutor-6
================taskExecutor-11
================taskExecutor-4
================taskExecutor-3
================taskExecutor-2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值