<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      CompletableFuture 類詳解

      1.runAsyncsupplyAsyncgetjoin:開啟異步任務(wù),獲取結(jié)果。
      // runAsync:沒有返回值
      CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
          System.out.println("Hello");
      });
      
      // supplyAsync:有返回值
      CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
          return "World";
      });
      
      // join:不強(qiáng)制 try,不能設(shè)置超時(shí)時(shí)間,不能響應(yīng)線程中斷
      System.out.println(future2.join());// World
      
      // get:必須 try 或 添加異常簽名,可以設(shè)置超時(shí)時(shí)間,可以響應(yīng)線程中斷
      try {
          System.out.println(future2.get());// World
      } catch (Exception ignored) {
      }
       
      2.thenApplythenAcceptthenRun:處理任務(wù)的結(jié)果。
      CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello");
      
      // thenRun:在任務(wù)完成后執(zhí)行
      CompletableFuture<Void> future2 = future.thenRun(() -> System.out.println("完成了"));
      
      // thenAccept:接收任務(wù)的結(jié)果
      CompletableFuture<Void> future3 = future.thenAccept(s -> System.out.println(s + " World"));// Hello World
      
      // thenApply:接收任務(wù)的結(jié)果,返回一個(gè)新值
      CompletableFuture<String> future4 = future.thenApply(s -> s + " World");
      
      System.out.println("------");
      System.out.println(future2.join());// null
      System.out.println(future3.join());// null
      System.out.println(future4.join());// Hello World
       
      3.thenCombinethenAcceptBothrunAfterBoth:處理兩個(gè)任務(wù)的結(jié)果。
      CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
      CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
      
      // runAfterBoth:在兩個(gè)任務(wù)完成后執(zhí)行
      CompletableFuture<Void> combinedFuture = future1.runAfterBoth(future2, () -> System.out.println("完成了"));
      
      // thenAcceptBoth:接收兩個(gè)任務(wù)的結(jié)果
      CompletableFuture<Void> combinedFuture2 = future1.thenAcceptBoth(future2, (s1, s2) -> System.out.println(s1 + " " + s2));// Hello World
      
      // thenCombine:接收兩個(gè)任務(wù)的結(jié)果,返回一個(gè)新值
      CompletableFuture<String> combinedFuture3 = future1.thenCombine(future2, (s1, s2) -> s1 + " " + s2);
      
      System.out.println("------");
      System.out.println(combinedFuture.join());// null
      System.out.println(combinedFuture2.join());// null
      System.out.println(combinedFuture3.join());// Hello World
       
      4.applyToEitheracceptEitherrunAfterEither:處理兩個(gè)任務(wù)任一的結(jié)果。
      CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> "Hello");
      CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> "World");
      
      // runAfterBoth:在兩個(gè)任務(wù)任一完成后執(zhí)行
      CompletableFuture<Void> combinedFuture = future1.runAfterEither(future2, () -> System.out.println("完成了"));
      
      // thenAcceptBoth:接收兩個(gè)任務(wù)的任一結(jié)果
      CompletableFuture<Void> combinedFuture2 = future1.acceptEither(future2, (s) -> System.out.println(s));// Hello 或 World
      
      // thenCombine:接收兩個(gè)任務(wù)的任一結(jié)果,返回一個(gè)新值
      CompletableFuture<String> combinedFuture3 = future1.applyToEither(future2, (s) -> s);
      
      System.out.println("------");
      System.out.println(combinedFuture.join());// null
      System.out.println(combinedFuture2.join());// null
      System.out.println(combinedFuture3.join());// Hello 或 World
       
      5.exceptionallywhenCompletehandle:處理異常。
      CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
          if(Math.random() > 0.5){
              throw new RuntimeException();
          }
          return "Hello";
      });
      
      // exceptionally:處理異常
      CompletableFuture<String> future2 = future.exceptionally(e -> {
          System.out.println("異常信息: " + e.getMessage());// 異常信息: java.lang.RuntimeException
          return null;
      });
      
      // whenComplete:處理異常和結(jié)果
      CompletableFuture<String> future3 = future.whenComplete((s, e) -> {
          if (e != null) {
              System.out.println("異常信息: " + e.getMessage());
          } else {
              System.out.println(s);
          }
      });
      
      // handle:處理異常和結(jié)果,返回新結(jié)果
      CompletableFuture<String> future4 = future.handle((s, e) -> {
          if (s != null) {
              return s + " World";
          } else {
              return "異常信息: " + e.getMessage();
          }
      });
      
      System.out.println("------");
      System.out.println(future2.join());// Hello 或 null
      System.out.println(future4.join());// Hello World 或 異常信息: java.lang.RuntimeException
      System.out.println(future3.join());// Hello World 或 直接報(bào)錯(cuò)退出

       

      6.allOfanyOf:等待 多個(gè) 或 任一 任務(wù)的完成。
      String[] arr = new String[2];
      CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
          arr[0] = "Hello";
          return "Hello";
      });
      CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() -> {
          arr[1] = "World";
          return "World";
      });
      
      // anyOf:等待任一任務(wù)完成
      CompletableFuture<Object> future3 = CompletableFuture.anyOf(future1, future2);
      System.out.println(arr[0] + " " + arr[1]);// Hello World 或 Hello null 或 null World
      
      // allOf:等待所有任務(wù)全部完成
      CompletableFuture<Void> future4 = CompletableFuture.allOf(future1, future2);
      System.out.println(arr[0] + " " + arr[1]);// Hello World
      
      System.out.println("------");
      System.out.println(future3.join());// Hello 或 World
      System.out.println(future4.join());// null

       

      posted @ 2024-04-28 20:07  Yfeil  閱讀(140)  評(píng)論(0)    收藏  舉報(bào)
      主站蜘蛛池模板: 国产欧亚州美日韩综合区| 久久国产精品波多野结衣av| 国产高清在线精品一本大道| 久久精品丝袜高跟鞋| 一卡2卡三卡4卡免费网站| 在线观看国产精品日韩av| 欧洲人与动牲交α欧美精品 | 丰满无码人妻热妇无码区| 亚洲午夜精品久久久久久抢| 精品国产中文字幕在线| 精品人妻系列无码天堂| 丰满人妻一区二区三区高清精品 | 92精品国产自产在线观看481页| 好湿好紧太硬了我太爽了视频| 日韩理伦片一区二区三区| 久久免费偷拍视频有没有| 国产11一12周岁女毛片| 日本一区不卡高清更新二区| 精品日本免费一区二区三区| 国产偷窥熟女高潮精品视频| 西和县| 蜜臀av一区二区三区日韩| 欧美日韩精品一区二区视频| 奶头又大又白喷奶水av| 精品国产熟女一区二区三区| 国产亚洲精品VA片在线播放| 国产欧美精品一区二区三区| 久久综合给合久久狠狠狠88| 国产精品一区久久人人爽| 日日摸夜夜添夜夜添国产三级| 亚洲国产精品毛片av不卡在线| 在线亚洲妇色中文色综合| 国产超碰无码最新上传| 97精品人妻系列无码人妻| 国产一区二区三区九九视频| 国产高清在线精品一本大道| 丁香五月亚洲综合深深爱| 在线视频一区二区三区色| 99国产精品欧美一区二区三区| 久9re热视频这里只有精品免费| 一区二区不卡99精品日韩|