接口并行案列

 @SneakyThrows
    public static void main(String[] args) {

        //自定义线程池
        ThreadPoolExecutor executorService = new ThreadPoolExecutor(
                8,
                10,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.CallerRunsPolicy());
        //使用自定义线程池
/*        CompletableFuture<DemandFile> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一开始");
            DemandFile file = new DemandFile();
            file.setId("1");
            return file;
        },executorService);*/
        //不加自定义线程池  使用默认的
        CompletableFuture<DemandFile> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务一开始");
            DemandFile file = new DemandFile();
            file.setId("1");
            return file;
        });
        CompletableFuture<DemandFile> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务二开始");
            DemandFile file = new DemandFile();
            file.setId("2");
            return file;
        });
        获取结果,有返回值  务一和任务二完成 才走这
        future1.thenAcceptBothAsync(future2, (result1, result2) -> {
            System.out.println("感知任务一和任务二完成,执行任务三"+result1+"-----"+result2);
        });

        //获取结果,有返回值
        CompletableFuture<String> future = future1.thenCombineAsync(future2, (f1, f2) -> {  //参数f1,f2分别对应了future01,future02的返回值
            return f1 + ":" + f2 + "-> hello";
        });

//        Thread.sleep(1000);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值