CompletableFuture异步工作编排

Lewis
2022-08-24 / 0 评论 / 11 阅读 / 正在检测是否收录...
public static void main(String[] args) {
        CompletableFuture<List> stepOne = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            List<Integer> stepOneList = new ArrayList();
            stepOneList.add(1);
            System.out.println("执行1结束");
            return stepOneList;
        });
        CompletableFuture<List> stepTwo = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            List<Integer> stepTwoList = new ArrayList();
            stepTwoList.add(2);
            System.out.println("执行2结束");
            return stepTwoList;
        });
        CompletableFuture<List> stepThree = CompletableFuture.supplyAsync(() -> {

            List<Integer> stepThreeList = new ArrayList();
            stepThreeList.add(3);
            System.out.println("执行3结束");
            return stepThreeList;
        });
        System.out.println("执行4结束");
        CompletableFuture<Void> all = CompletableFuture.allOf(stepOne, stepTwo, stepThree);
        all.join();
        try {
            System.out.println(stepOne.get());
            System.out.println(stepTwo.get());
            System.out.println(stepThree.get());

        } catch (Exception e){

        }


    }
0

评论 (0)

取消