CompletableFuture 入門學習


CompletableFuture 是JDK1.8 版本出現的異步編程函數,實現 Future 和 CompletionStage 接口

將從一下幾個方面學習 CompletableFuture 並更好的理解並發編程思想

image-20210105223522027

應用場景1:創建異步對象

CompletableFuture 提供了四個靜態方法來創建一個異步任務

public static CompletableFuture<Void> runAsync(Runnable runnable);
public static CompletableFuture<Void> runAsync(Runnable runnable, Executor executor);
 
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier) ;
public static <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier, Executor executor);

1、runXxxx 都是沒有返回結果的,supplyXxxx都是可以獲取返回結果的

2、提供兩個入參的方法可以傳入自定義的線程池,否則默認使用公用的ForkJoinPool.commonPool()作為執行異步任務的線程池

示例代碼

public static void demo1() throws ExecutionException, InterruptedException {
    System.out.println("主線程開始執行。。。");
    CompletableFuture<Void> future01 = CompletableFuture.runAsync(() -> System.out.println("無返回值,使用默認線程池"));
    System.out.println(future01.get());

    CompletableFuture<Void> future02 = CompletableFuture.runAsync(() -> System.out.println("無返回值,使用自定義線程池"), executor);
    System.out.println(future02.get());

    CompletableFuture<Long> future03 = CompletableFuture.supplyAsync(() -> {
        System.out.println("有返回值,使用默認線程池");
        return System.currentTimeMillis();
    });
    System.out.println(future03.get());

    CompletableFuture<Long> future04 = CompletableFuture.supplyAsync(() -> {
        System.out.println("有返回值,使用自定義線程池");
        return System.currentTimeMillis();
    }, executor);
    System.out.println(future04.get());

    System.out.println("主線程結束。。。");
}

應用場景2:異步任務完成時回調方法

當CompletableFuture的計算結果完成,或者拋出異常的時候,可以執行特定的Action。主要是下面的方法:

public CompletableFuture<T> whenComplete(BiConsumer<? super T,? super Throwable> action);
返回相同的結果或例外,這一階段的新completionstage,這個階段完成時,執行特定動作的結果(或 null如果沒有)和異常(或 null如果沒有)這個階段。 

public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action);
返回相同的結果或例外,這一階段的新completionstage,這個階段完成時,執行特定動作執行給定的操作這一階段的默認的異步執行設施,其結果(或 null如果沒有)和異常(或 null如果沒有)這個階段作為參數。

public CompletableFuture<T> whenCompleteAsync(BiConsumer<? super T,? super Throwable> action, Executor executor);
返回相同的結果或例外,這一階段的新completionstage,這個階段完成時,執行使用所提供的遺囑執行人,給出的行動與結果(或 null如果沒有)和異常(或 null如果沒有)這個階段作為參數。 

public CompletableFuture<T> exceptionally(Function<Throwable,? extends T> fn);
返回一個新的completablefuture已經完成與給定值。 

whenComplete 可以處理正常和異常的計算結果,exceptionally 處理異常情況

whenComplete 和 whenCompleteAsync 的區別:

  • whenComplete 是執行當前任務的線程繼續執行回調任務
  • whenCompleteAsync 是把回調任務提交給線程池來繼續執行

方法不以 Async 結尾,意味着 Action 使用相同的線程池執行,而 Async 可能會使用其他線程執行(如果是使用相同的線程池,也可能會被同一個線程選中執行)

代碼示例

public static void demo2() throws ExecutionException, InterruptedException {
    
    System.out.println("主線程開始執行。。。線程id:" + Thread.currentThread().getId());
    CompletableFuture<Long> future01 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future01執行,線程id:" + Thread.currentThread().getId());
        return System.currentTimeMillis();
    }).whenComplete((res, e) -> {
        System.out.println("future01回調方法執行 ==》" + res + ",線程id:" + Thread.currentThread().getId());
    });
    System.out.println(future01.get());

    CompletableFuture<Long> future02 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future02執行,線程id:" + Thread.currentThread().getId());
        return System.currentTimeMillis();
    }).whenCompleteAsync((res, e) -> {
        System.out.println("future02回調方法執行 ==》" + res + ",線程id:" + Thread.currentThread().getId());
    });
    System.out.println(future02.get());

    CompletableFuture<Long> future03 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future03執行,線程id:" + Thread.currentThread().getId());
        return System.currentTimeMillis();
    }).whenCompleteAsync((res, e) -> {
        System.out.println("future03回調方法執行 ==》" + res + ",線程id:" + Thread.currentThread().getId());
    }, executor);
    System.out.println(future03.get());

    CompletableFuture<Long> future04 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future04執行,線程id:" + Thread.currentThread().getId());
        int i = 10 / 0;
        return System.currentTimeMillis();
    }).whenCompleteAsync((res, e) -> {
        System.out.println("future04回調方法執行 ==》" + res + ",線程id:" + Thread.currentThread().getId());
    }, executor).exceptionally((e) -> {
        System.out.println("異常回調執行,異常原因:" + e.getMessage() + "==> 線程id:" + Thread.currentThread().getId());
        return 0L;
    });
    System.out.println(future04.get());

    System.out.println("主線程結束。。。");
}

應用場景3:線程串行化方法

public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn)
public <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
當一個線程依賴另一個線程時,獲取上一個任務返回的結果,**並返回當前任務的返回值**。(有返回值)

public CompletionStage<Void> thenAccept(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action);
public CompletionStage<Void> thenAcceptAsync(Consumer<? super T> action,Executor executor);
消費處理結果。接收任務的處理結果,並消費處理,**無返回結果。

public CompletionStage<Void> thenRun(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action);
public CompletionStage<Void> thenRunAsync(Runnable action,Executor executor);
只要上面的任務執行完成,就開始執行thenRun,**只是處理完任務后,執行 thenRun的后續操作

  • thenApply 方法:當一個線程依賴另一個線程時,獲取上一個任務返回的結果,並返回當前任務的返回值

  • thenAccept方法:消費處理結果。接收任務的處理結果,並消費處理,無返回結果。

  • thenRun方法:只要上面的任務執行完成,就開始執行thenRun,只是處理完任務后,執行 thenRun的后續操作

每一個方法都對應了三種操作。帶有Async默認是異步執行的。這里所謂的異步指的是不在當前線程內執行。帶有參數Executor executor的則用自定義的線程池,不指定的話則用默認ForkJoinPool.commonPool()線程池

代碼示例

public static void demo3() throws ExecutionException, InterruptedException {
    System.out.println("主線程開始執行。。。線程id:" + Thread.currentThread().getId());
    CompletableFuture<Void> future01 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future01執行,線程id:" + Thread.currentThread().getId());
        return System.currentTimeMillis();
    }).thenRun(() -> {
        System.out.println("thenRun執行,無法獲取上一步執行結果,無返回值");
    });
    System.out.println(future01.get());

    CompletableFuture<Void> future02 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future01執行,線程id:" + Thread.currentThread().getId());
        return System.currentTimeMillis();
    }).thenAccept((res) -> {
        System.out.println("thenAccept,上一步執行結果: " + res +",無返回值");
    });
    System.out.println(future02.get());

    CompletableFuture<Long> future03 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future03執行,線程id:" + Thread.currentThread().getId());
        return System.currentTimeMillis();
    }).thenApply((res) -> {
        long i = 10;
        System.out.println("thenApply,上一步執行結果: " + res + ",有返回值: " + i);
        return i;
    });
    System.out.println(future03.get());

    System.out.println("主線程結束。。。");
}

應用場景4:兩任務組合 - 都要完成

public <U,V> CompletableFuture<V> thenCombine(
    CompletionStage<? extends U> other,
    BiFunction<? super T,? super U,? extends V> fn);

public <U,V> CompletableFuture<V> thenCombineAsync(
    CompletionStage<? extends U> other,
    BiFunction<? super T,? super U,? extends V> fn);

public <U,V> CompletableFuture<V> thenCombineAsync(
    CompletionStage<? extends U> other,
    BiFunction<? super T,? super U,? extends V> fn, Executor executor);
 組合兩個future,獲取兩個future任務的返回結果,並返回當前任務的返回值   
     

public <U> CompletableFuture<Void> thenAcceptBoth(
    CompletionStage<? extends U> other,
    BiConsumer<? super T, ? super U> action);

public <U> CompletableFuture<Void> thenAcceptBothAsync(
    CompletionStage<? extends U> other,
    BiConsumer<? super T, ? super U> action);

public <U> CompletableFuture<Void> thenAcceptBothAsync(
    CompletionStage<? extends U> other,
    BiConsumer<? super T, ? super U> action, Executor executor);
組合兩個future,獲取兩個future任務的返回結果,然后處理任務,沒有返回值。
    

public CompletableFuture<Void> runAfterBoth(CompletionStage<?> other, Runnable action);

public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action);

public CompletableFuture<Void> runAfterBothAsync(CompletionStage<?> other, Runnable action,
                                                 Executor executor);
組合兩個future,不需要獲取future的結果,只需兩個future處理完任務后,處理該任務

  • thenCombine:組合兩個future,獲取兩個future任務的返回結果,並返回當前任務的返回值

  • thenAcceptBoth:組合兩個future,獲取兩個future任務的返回結果,然后處理任務,沒有返回值。

  • runAfterBoth:組合兩個future,不需要獲取future的結果,只需兩個future處理完任務后,處理該任務

示例代碼

public static void demo4() throws ExecutionException, InterruptedException {
    System.out.println("主線程開始執行。。。線程id:" + Thread.currentThread().getId());

    CompletableFuture<Long> future01 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future01執行,線程id:" + Thread.currentThread().getId());
        System.out.println("future01結束");
        return System.currentTimeMillis();
    });
    CompletableFuture<Long> future02 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future02執行,線程id:" + Thread.currentThread().getId());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("future02結束");
        return System.currentTimeMillis();
    });

    CompletableFuture<Void> future03 = future01.runAfterBoth(future02, () -> {
        System.out.println("任務3開始。。。");
    });

    CompletableFuture<Void> future04 = future01.thenAcceptBoth(future02, (f1, f2) -> {
        System.out.println("任務4開始。。。之前的結果 "+f1+"==>"+f2);
    });

    CompletableFuture<Long> future05 = future01.thenCombine(future02, (f1, f2) -> {
        System.out.println("任務5開始。。。之前的結果 "+f1+"==>"+f2);
        return 0L;
    });
    System.out.println(future05.get());


    System.out.println("主線程結束。。。");
}

應用場景5:兩任務組合 - 任意一個完成

public <U> CompletableFuture<U> applyToEither(
    CompletionStage<? extends T> other, Function<? super T, U> fn);

public <U> CompletableFuture<U> applyToEitherAsync(
    CompletionStage<? extends T> other, Function<? super T, U> fn);

public <U> CompletableFuture<U> applyToEitherAsync(
    CompletionStage<? extends T> other, Function<? super T, U> fn,Executor executor) ;
 組合兩個future,只需一個future任務的返回結果,並返回當前任務的返回值   
     

public <U> CompletableFuture<Void> acceptEither(
    CompletionStage<? extends U> other,
    BiConsumer<? super T, ? super U> action);

public <U> CompletableFuture<Void> acceptEitherAsync(
    CompletionStage<? extends U> other,
    BiConsumer<? super T, ? super U> action);

public <U> CompletableFuture<Void> acceptEitherAsync(
    CompletionStage<? extends U> other,
    BiConsumer<? super T, ? super U> action, Executor executor);
組合兩個future,只需一個future任務的返回結果,然后處理任務,沒有返回值。
    

public CompletableFuture<Void> runAfterEither(CompletionStage<?> other, Runnable action);

public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action);

public CompletableFuture<Void> runAfterEitherAsync(CompletionStage<?> other, Runnable action,
                                                 Executor executor);
組合兩個future,不需要獲取future的結果,只需一個future處理完任務后,處理該任務
  • applyToEither:組合兩個future,只需一個future任務的返回結果,並返回當前任務的返回值

  • acceptEither:組合兩個future,只需一個future任務的返回結果,然后處理任務,沒有返回值。

  • runAfterEither:組合兩個future,不需要獲取future的結果,只需一個future處理完任務后,處理該任務

示例代碼

public static void demo5() throws ExecutionException, InterruptedException {
    System.out.println("主線程開始執行。。。線程id:" + Thread.currentThread().getId());

    CompletableFuture<Long> future01 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future01執行,線程id:" + Thread.currentThread().getId());
        System.out.println("future01結束");
        return System.currentTimeMillis();
    });
    CompletableFuture<Long> future02 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future02執行,線程id:" + Thread.currentThread().getId());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("future02結束");
        return System.currentTimeMillis();
    });

    CompletableFuture<Void> future03 = future01.runAfterEither(future02, () -> {
        System.out.println("任務3開始。。。");
    });

    CompletableFuture<Void> future04 = future01.acceptEither(future02, (res) -> {
        System.out.println("任務4開始。。。之前的結果 "+res);
    });

    CompletableFuture<Long> future05 = future01.applyToEither(future02, (res) -> {
        System.out.println("任務5開始。。。之前的結果 "+res);
        return 0L;
    });
    System.out.println(future05.get());


    System.out.println("主線程結束。。。");
}

應用場景6:多任務組合

public static CompletableFuture<Void> allOf(CompletableFuture<?>... cfs);

public static CompletableFuture<Object> anyOf(CompletableFuture<?>... cfs);
  • allOf:等待所有任務完成

  • anyOf:只要有一個任務完成,並返回執行結果

代碼示例

public static void demo6() throws ExecutionException, InterruptedException {
    System.out.println("主線程開始執行。。。線程id:" + Thread.currentThread().getId());

    CompletableFuture<Long> future01 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future01執行,線程id:" + Thread.currentThread().getId());
        System.out.println("future01結束");
        return System.currentTimeMillis();
    });
    CompletableFuture<Long> future02 = CompletableFuture.supplyAsync(() -> {
        System.out.println("future02執行,線程id:" + Thread.currentThread().getId());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("future02結束");
        return System.currentTimeMillis();
    });

    CompletableFuture<Void> future03 = CompletableFuture.allOf(future01, future02);
    CompletableFuture<Object> future04 = CompletableFuture.anyOf(future01, future02);

    System.out.println("anyOf 任意一個異步任務執行完成"+future04.get());
    System.out.println("allOf 等待所有異步任務執行完成"+future03.get());

    System.out.println("主線程結束。。。");
}

完整案例代碼:

https://github.com/dongtiandexue/love-code/blob/master/java-base/src/main/java/com/java/base/concurrent/CompletableFutureTest.java


免責聲明!

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



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