CompletableFuture异步编排 java8

 @Autowired
 private ThreadPoolExecutor executor;

//开启线程  supplyAsync() 是可以拿到任务执行够的返回值 
//runAsync() 返回的是void
 CompletableFuture<Void> task01= CompletableFuture.runAsync(() -> {
 // 业务代码
}, executor);

 CompletableFuture<Void> task02= CompletableFuture.runAsync(() -> {
 // 业务代码
}, executor);

 CompletableFuture<Void> task03= CompletableFuture.runAsync(() -> {
 // 业务代码
}, executor);

// 阻塞等待所有的任务执行完成 才可以执行下一步
// anyOf()等待其中一个线程完成就可以执行主线程了
 CompletableFuture.allOf(task01, task02, task03).get();
//主线程执行
return null;


===========注入线程池===============
@Configuration
public class MyThreadConfig {

    @Bean
    @Primary
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties pool) {
        return new ThreadPoolExecutor(pool.getCoreSize(),
                pool.getMaxSize(), pool.getKeepAliveTime(),
                TimeUnit.SECONDS, new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
    }
}

@ConfigurationProperties(prefix = "xxx.thread")
@Component
@Data
public class ThreadPoolConfigProperties {
    private Integer coreSize;
    private Integer maxSize;
    private Integer keepAliveTime;
}
yaml 位置加上下面参数
xxx:
  thread:
    core-size: 20
    max-size: 200
    keep-alive-time: 10
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值