diff --git a/hutool-core/src/main/java/cn/hutool/core/thread/SyncUtil.java b/hutool-core/src/main/java/cn/hutool/core/thread/SyncUtil.java new file mode 100644 index 000000000..5f4601b39 --- /dev/null +++ b/hutool-core/src/main/java/cn/hutool/core/thread/SyncUtil.java @@ -0,0 +1,53 @@ +package cn.hutool.core.thread; + +import cn.hutool.core.exceptions.ExceptionUtil; + +import java.lang.reflect.UndeclaredThrowableException; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + +/** + * CompletableFuture工具类,叫CompletableFutureUtil太长 + * + * @author + * @since 2021/11/10 0010 20:55 + */ +public class SyncUtil { + + private SyncUtil() { + /* Do not new me! */ + } + + /** + * 等待所有任务执行完毕,包裹了异常 + * + * @param tasks 并行任务 + * @throws UndeclaredThrowableException 未受检异常 + */ + public static void wait(CompletableFuture... tasks) { + try { + CompletableFuture.allOf(tasks).get(); + } catch (InterruptedException | ExecutionException e) { + ExceptionUtil.wrapAndThrow(e); + } + } + + /** + * 获取异步任务结果,包裹了异常 + * + * @param task 异步任务 + * @param 任务返回值类型 + * @return 任务返回值 + * @throws RuntimeException 未受检异常 + */ + public static T get(CompletableFuture task) { + RuntimeException exception; + try { + return task.get(); + } catch (InterruptedException | ExecutionException e) { + exception = ExceptionUtil.wrapRuntime(e); + } + throw exception; + } + +}