<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ### 結合 CompletableFuture 第二種類型的 `CompletableFuture` 方法采用兩種 `CompletableFuture` 并以各異方式將它們組合在一起。就像兩個人在比賽一樣, 一個`CompletableFuture`通常比另一個更早地到達終點。這些方法允許你以不同的方式處理結果。 為了測試這一點,我們將創建一個任務,它有一個我們可以控制的定義了完成任務所需要的時間量的參數。 CompletableFuture 先完成: ```java // concurrent/Workable.java import java.util.concurrent.*; import onjava.Nap; public class Workable { String id; final double duration; public Workable(String id, double duration) { this.id = id; this.duration = duration; } @Override public String toString() { return "Workable[" + id + "]"; } public static Workable work(Workable tt) { new Nap(tt.duration); // Seconds tt.id = tt.id + "W"; System.out.println(tt); return tt; } public static CompletableFuture<Workable> make(String id, double duration) { return CompletableFuture .completedFuture( new Workable(id, duration) ) .thenApplyAsync(Workable::work); } } ``` 在 `make()`中,`work()`方法應用于`CompletableFuture`。`work()`需要一定的時間才能完成,然后它將字母W附加到id上,表示工作已經完成。 現在我們可以創建多個競爭的 `CompletableFuture`,并使用 `CompletableFuture` 庫中的各種方法來進行操作: ```java // concurrent/DualCompletableOperations.java import java.util.concurrent.*; import static onjava.CompletableUtilities.*; public class DualCompletableOperations { static CompletableFuture<Workable> cfA, cfB; static void init() { cfA = Workable.make("A", 0.15); cfB = Workable.make("B", 0.10); // Always wins } static void join() { cfA.join(); cfB.join(); System.out.println("*****************"); } public static void main(String[] args) { init(); voidr(cfA.runAfterEitherAsync(cfB, () -> System.out.println("runAfterEither"))); join(); init(); voidr(cfA.runAfterBothAsync(cfB, () -> System.out.println("runAfterBoth"))); join(); init(); showr(cfA.applyToEitherAsync(cfB, w -> { System.out.println("applyToEither: " + w); return w; })); join(); init(); voidr(cfA.acceptEitherAsync(cfB, w -> { System.out.println("acceptEither: " + w); })); join(); init(); voidr(cfA.thenAcceptBothAsync(cfB, (w1, w2) -> { System.out.println("thenAcceptBoth: " + w1 + ", " + w2); })); join(); init(); showr(cfA.thenCombineAsync(cfB, (w1, w2) -> { System.out.println("thenCombine: " + w1 + ", " + w2); return w1; })); join(); init(); CompletableFuture<Workable> cfC = Workable.make("C", 0.08), cfD = Workable.make("D", 0.09); CompletableFuture.anyOf(cfA, cfB, cfC, cfD) .thenRunAsync(() -> System.out.println("anyOf")); join(); init(); cfC = Workable.make("C", 0.08); cfD = Workable.make("D", 0.09); CompletableFuture.allOf(cfA, cfB, cfC, cfD) .thenRunAsync(() -> System.out.println("allOf")); join(); } } ``` **輸出結果**: ``` Workable[BW] runAfterEither Workable[AW] ***************** Workable[BW] Workable[AW] runAfterBoth ***************** Workable[BW] applyToEither: Workable[BW] Workable[BW] Workable[AW] ***************** Workable[BW] acceptEither: Workable[BW] Workable[AW] ***************** Workable[BW] Workable[AW] thenAcceptBoth: Workable[AW], Workable[BW] **************** Workable[BW] Workable[AW] thenCombine: Workable[AW], Workable[BW] Workable[AW] ***************** Workable[CW] anyOf Workable[DW] Workable[BW] Workable[AW] ***************** Workable[CW] Workable[DW] Workable[BW] Workable[AW] ***************** allOf ``` - 為了方便訪問, 將 `cfA` 和 `cfB` 定義為 `static`的。 - `init()`方法用于 `A`, `B` 初始化這兩個變量,因為 `B` 總是給出比`A`較短的延遲,所以總是 `win` 的一方。 - `join()` 是在兩個方法上調用 `join()` 并顯示邊框的另一個便利方法。 - 所有這些 “`dual`” 方法都以一個 `CompletableFuture` 作為調用該方法的對象,第二個 `CompletableFuture` 作為第一個參數,然后是要執行的操作。 - 通過使用 `showr()` 和 `voidr()` 可以看到,“`run`”和“`accept`”是終端操作,而“`apply`”和“`combine`”則生成新的 `payload-bearing` (承載負載)的 `CompletableFuture`。 - 方法的名稱不言自明,你可以通過查看輸出來驗證這一點。一個特別有趣的方法是 `combineAsync()`,它等待兩個 `CompletableFuture` 完成,然后將它們都交給一個 `BiFunction`,這個 `BiFunction` 可以將結果加入到最終的 `CompletableFuture` 的有效負載中。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看