zoukankan      html  css  js  c++  java
  • 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===");
                    });
        }
    }
  • 相关阅读:
    elasticsearch的基本用法
    JavaScript实现拖拽预览,AJAX小文件上传
    php四排序-选择排序
    php四排序-冒泡排序
    centos6.5编译安装lamp开发环境
    centos7.2 yum安装lamp环境
    chrome升级54以后,显示Adobe Flash Player 因过期而遭到阻止
    chrome45以后的版本安装lodop后,仍提示未安装解决
    APACHE重写去除入口文件index.php
    PHP之factory
  • 原文地址:https://www.cnblogs.com/moris5013/p/12019941.html
Copyright © 2011-2022 走看看