异步操作之 CompletableFuture

CompletableFuture 也可以异步执行方法

1、无返回结果的示例

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

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

2、返回基本类型的示例

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);

猜你喜欢

转载自blog.csdn.net/qq_38428623/article/details/103094274