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===");
                });
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM