CompletableFuture使用场景


例题来源于 b站灰灰

https://space.bilibili.com/51950540/dynamic?spm_id_from=444.41.0.0

场景1 主线程等待子线程执行完毕后并获取结果而后执行

public class Test {
    public static void main(String[] args) {
        SmallTools.printTimeAndThread("小白进入餐厅");
        SmallTools.printTimeAndThread("小白点了 番茄炒蛋+一碗米饭");
        CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> {
            SmallTools.printTimeAndThread("厨师炒菜");
            SmallTools.sleep(200);
            SmallTools.printTimeAndThread("厨师打饭");
            SmallTools.sleep(300);
            return "番茄鸡蛋+米饭做好啦";
        });
        SmallTools.printTimeAndThread("小白在打王者");
        SmallTools.printTimeAndThread(String.format("%s,小白开吃", cf1.join()));
    }

    static class SmallTools {
        public static void sleep(long millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static String printTimeAndThread(String tag) {
            String result = new StringJoiner("\t|\t")
                    .add(String.valueOf(System.currentTimeMillis()))
                    .add(String.valueOf(Thread.currentThread().getId()))
                    .add(Thread.currentThread().getName())
                    .add(tag)
                    .toString();
            System.out.println(result);
            return result;
        }
    }
}
  • supplyAsync 用来开启一个异步任务
    其中 CompletableFuture.supplyAsync 用来开启一个子线程,其方法执行完毕后返回一个结果,主线程可以通过调用 cf1.join() 来获取执行结果

场景2 主线程等待多个子线程执行完毕后执行,而子线程分先后顺序,线程1需要等待线程2执行完毕后再执行

public class Test {
    public static void main(String[] args) {
        SmallTools.printTimeAndThread("小白进入餐厅");
        SmallTools.printTimeAndThread("小白点了 番茄炒蛋+一碗米饭");
        CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> {
            SmallTools.printTimeAndThread("厨师炒菜");
            SmallTools.sleep(1200);
            return "番茄鸡蛋";
        }).thenCompose(dish -> CompletableFuture.supplyAsync(() -> {
            SmallTools.printTimeAndThread("服务员打饭");
            SmallTools.sleep(300);
            return dish + "+米饭做好啦";
        }));
        SmallTools.printTimeAndThread("小白在打王者");
        SmallTools.printTimeAndThread(String.format("%s,小白开吃", cf1.join()));
    }

    static class SmallTools {
        public static void sleep(long millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static String printTimeAndThread(String tag) {
            String result = new StringJoiner("\t|\t")
                    .add(String.valueOf(System.currentTimeMillis()))
                    .add(String.valueOf(Thread.currentThread().getId()))
                    .add(Thread.currentThread().getName())
                    .add(tag)
                    .toString();
            System.out.println(result);
            return result;
        }
    }
}

  • thenCompose 用来连接两个任务
    第二个任务需要获取第一个任务的返回结果后返回

场景3 子线程等待两个子线程执行完毕后,再将这两个子线程的执行结果统一返回

public class Test {
    public static void main(String[] args) {
        SmallTools.printTimeAndThread("小白进入餐厅");
        SmallTools.printTimeAndThread("小白点了 番茄炒蛋+一碗米饭");
        CompletableFuture cf1 = CompletableFuture.supplyAsync(() -> {
            SmallTools.printTimeAndThread("厨师炒菜");
            SmallTools.sleep(200);
            return "番茄鸡蛋";
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            SmallTools.printTimeAndThread("服务员煮饭");
            SmallTools.sleep(200);
            return "";
        }), (dish, rice) -> {
            SmallTools.printTimeAndThread("服务员打饭");
            return dish + rice;
        });
        SmallTools.printTimeAndThread("小白在打王者");
        SmallTools.printTimeAndThread(String.format("%s,小白开吃", cf1.join()));
    }

    static class SmallTools {
        public static void sleep(long millis) {
            try {
                Thread.sleep(millis);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public static String printTimeAndThread(String tag) {
            String result = new StringJoiner("\t|\t")
                    .add(String.valueOf(System.currentTimeMillis()))
                    .add(String.valueOf(Thread.currentThread().getId()))
                    .add(Thread.currentThread().getName())
                    .add(tag)
                    .toString();
            System.out.println(result);
            return result;
        }
    }
}
  • thenCombine 连接两个子线程,而后将两个线程的执行结果合并后统一输入