• CompletableFuture1


    public class CompletableFutureTest {
        public static void main(String[] args) throws Exception {
    
            test5();
    
        }
    
        /**
         * whenCompleteAsync指的是异步执行传入的BiConsumer
         * whenComplete 指的是同步执行传入的BiConsumer
         */
        public static void  test1() throws ExecutionException, InterruptedException {
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "hello");
             //future.whenCompleteAsync((v, r) -> {
            future.whenComplete((v, r) -> {
                System.out.println("=========");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("====over=====");
            });
            System.out.println("^^^^^^^^^^");
            System.out.println(future.get());
            Thread.currentThread().join();
        }
    
    
        /**
         * 同样有异步和同步两种方法,thenApply没有异常处理
         * @throws ExecutionException
         * @throws InterruptedException
         */
        public static void  test2() throws ExecutionException, InterruptedException {
            CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> "hello")
                    .thenApply((s) -> {
                        try {
                            System.out.println("==========");
                            TimeUnit.SECONDS.sleep(5);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println("====over=====");
                        return s.length();
                    });
    //              .thenApplyAsync((s) -> {
    //                    try {
    //                        System.out.println("==========");
    //                        TimeUnit.SECONDS.sleep(5);
    //                    } catch (InterruptedException e) {
    //                        e.printStackTrace();
    //                    }
    //                    System.out.println("====over=====");
    //                    return s.length();
    //                });
            System.out.println("^^^^^^^^^^");
            System.out.println(future.get());
            Thread.currentThread().join();
        }
    
        /**
         * handleAsync 有异常处理
         * @throws ExecutionException
         * @throws InterruptedException
         */
        public static void  test3() throws ExecutionException, InterruptedException {
            CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> "hello")
                    .handleAsync((v, t) -> {
                        return v.length();
                    });
            System.out.println(future.get());
        }
    
        /**
         * thenAcceptAsync 直接将上一个的结果进行消费
         * @throws ExecutionException
         * @throws InterruptedException
         */
        public static void  test4() throws ExecutionException, InterruptedException {
            CompletableFuture.supplyAsync(() -> "hello")
                    .thenAcceptAsync((x) -> {
                        System.out.println(x);
                    });
        }
    
        /**
         *执行完上一个future后再执行一个runnable
         * @throws ExecutionException
         * @throws InterruptedException
         */
        public static void  test5() throws ExecutionException, InterruptedException {
            CompletableFuture.supplyAsync(() -> "hello")
                    .thenRunAsync(() -> {
                        System.out.println("====over===");
                    });
        }
    }
  • 相关阅读:
    C# 使用Lazy 懒加载
    软件测试用例报告整理
    什么是事务,为什么要引入事务?
    WPF+DataGrid+MySQL实现增删改查、Excel文件导出
    git 设置远程库别名
    Access Like内存溢出问题排查,并找出相应的包含26个日文片假名数据的sql语句
    java 离线中文语音文字识别
    fbx的法线设置方式引起的显示不正确
    C#定时job
    CSS3+js实现循环滚动文字播放与暂停demo
  • 原文地址:https://www.cnblogs.com/moris5013/p/12019941.html
Copyright © 2020-2023  润新知