CompletableFutureの非同期操作

CompletableFutureはメソッドを非同期で実行することもできます

1.無返還結果の例

final CompletableFuture <Void> future = CompletableFuture.runAsync(()-> { 
   log.info( "戻り回Void的CompletableFuture画像表示例"); 
})。exceptionally(
   throwable- > { log.error(throwable.getMessage()); 
   nullを返す; 
}); 

future.get(); 
RedisClientTest.log.info( "-------結束---------");

2.基本型を返す例

final CompletableFuture <String> future = CompletableFuture.supplyAsync(()-> { 
   return "返回文字列的CompletableFuture画像表示例"; 
})。exceptionally(
   throwable- > { log.info(throwable.getMessage()); 
   return "-1 "; 
}); 
最終的な文字列の結果= future.get(); 
log.info( "結果:" +結果);

 

おすすめ

転載: blog.csdn.net/qq_38428623/article/details/103094274