CompletableFuture of asynchronous operation

CompletableFuture can also execute methods asynchronously

1. Example of no return result

final CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
   log.info("返回 Void 的 CompletableFuture 操作示例");
}).exceptionally(throwable -> {
   log.error(throwable.getMessage());
   return null;
});

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

2. Examples of returning basic types

final CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
   return "返回 String 的 CompletableFuture 操作示例";
}).exceptionally(throwable -> {
   log.info(throwable.getMessage());
   return "-1";
});
final String result = future.get();
log.info("result:" + result);

 

Guess you like

Origin blog.csdn.net/qq_38428623/article/details/103094274