diff --git a/README.md b/README.md index 9ebd9e2..7f3e05d 100644 --- a/README.md +++ b/README.md @@ -20,14 +20,14 @@ xtool 是一个小小的 Java 工具集,遵循简单、可靠的原则,不 com.igeeksky.xtool xtool - 1.2.0 + 1.2.1 ``` ### 2.2.Gradle ```groovy -implementation group: 'com.igeeksky.xtool', name: 'xtool', version: '1.2.0' +implementation group: 'com.igeeksky.xtool', name: 'xtool', version: '1.2.1' ``` ### 2.3.编译安装 @@ -82,7 +82,11 @@ mvn clean install ### 1.2.0 -* 调整 Futures 接口参数 +* `Futures` InterruptedException: interrupt currentThread + +### 1.2.0 + +* change `Futures` API params ### 1.1.3 diff --git a/docs/Reference.md b/docs/Reference.md index 18c5824..e3f4906 100644 --- a/docs/Reference.md +++ b/docs/Reference.md @@ -1,6 +1,6 @@ ## xtool 参考文档 -Author: [Patrick.Lau](mailto:patricklauxx@gmail.com) Version: 1.2.0 +Author: [Patrick.Lau](mailto:patricklauxx@gmail.com) Version: 1.2.1 [![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) [![Release](https://img.shields.io/github/v/release/patricklaux/xtool)](https://github.com/patricklaux/xtool/releases) [![Maven Central](https://img.shields.io/maven-central/v/com.igeeksky.xtool/xtool.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22com.igeeksky.xtool%22%20AND%20a:%22xtool%22) [![codecov](https://codecov.io/gh/patricklaux/xtool/branch/main/graph/badge.svg?token=VJ87A1IAVH)](https://codecov.io/gh/patricklaux/xtool) [![Last commit](https://img.shields.io/github/last-commit/patricklaux/xtool)](https://github.com/patricklaux/xtool/commits) [![Join the chat at https://gitter.im/igeeksky/xtool](https://badges.gitter.im/igeeksky/xtool.svg)](https://gitter.im/igeeksky/xtool?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) @@ -26,14 +26,14 @@ xtool 是一个小小的 Java 工具集,遵循简单、可靠的原则,不 com.igeeksky.xtool xtool - 1.2.0 + 1.2.1 ``` #### 1.2.2.Gradle ```groovy -implementation group: 'com.igeeksky.xtool', name: 'xtool', version: '1.2.0' +implementation group: 'com.igeeksky.xtool', name: 'xtool', version: '1.2.1' ``` #### 1.2.3.编译安装 diff --git a/pom.xml b/pom.xml index 0fb9fb7..d37fbcc 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.igeeksky.xtool xtool - 1.2.0 + 1.2.1 xtool xtool is a very small set of Java tools. https://github.com/patricklaux/xtool diff --git a/src/main/java/com/igeeksky/xtool/core/concurrent/Futures.java b/src/main/java/com/igeeksky/xtool/core/concurrent/Futures.java index 5fa74b9..d2d0ea4 100644 --- a/src/main/java/com/igeeksky/xtool/core/concurrent/Futures.java +++ b/src/main/java/com/igeeksky/xtool/core/concurrent/Futures.java @@ -20,6 +20,42 @@ public final class Futures { private Futures() { } + /** + * 等待任务完成并返回任务执行结果(无时间限制) + * + * @param future 任务 + * @return 任务执行结果 + */ + public static T await(Future future) { + try { + return future.get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ConcurrentException("Interrupted", e); + } catch (ExecutionException e) { + throw new ConcurrentException(e.getMessage(), e.getCause()); + } + } + + /** + * 等待任务完成并返回任务执行结果(无时间限制) + * + * @param future 任务 + * @return 任务执行结果 + */ + public static T await(Future future, long timeout, TimeUnit unit) { + try { + return future.get(timeout, unit); + } catch (TimeoutException e) { + throw new ConcurrentException("Timeout:wait " + timeout + unit.name(), e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ConcurrentException("Interrupted", e); + } catch (ExecutionException e) { + throw new ConcurrentException(e.getMessage(), e.getCause()); + } + } + /** * 等待所有任务完成(无时间限制) *

@@ -49,8 +85,11 @@ public static void awaitAll(Future[] futures, int start) { future.get(); } } - } catch (InterruptedException | ExecutionException e) { - throw new ConcurrentException(e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ConcurrentException("Interrupted", e); + } catch (ExecutionException e) { + throw new ConcurrentException(e.getMessage(), e.getCause()); } } @@ -83,8 +122,11 @@ public static void awaitAll(ArrayList> futures, int start) { future.get(); } } - } catch (InterruptedException | ExecutionException e) { - throw new ConcurrentException(e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ConcurrentException("Interrupted", e); + } catch (ExecutionException e) { + throw new ConcurrentException(e.getMessage(), e.getCause()); } } @@ -135,8 +177,11 @@ public static int awaitAll(Future[] futures, long timeout, TimeUnit unit, int return i; } catch (TimeoutException e) { return i; - } catch (Exception e) { - throw new ConcurrentException(e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ConcurrentException("Interrupted", e); + } catch (ExecutionException e) { + throw new ConcurrentException(e.getMessage(), e.getCause()); } } @@ -186,8 +231,11 @@ public static int awaitAll(ArrayList> futures, long timeout, TimeUnit return i; } catch (TimeoutException e) { return i; - } catch (Exception e) { - throw new ConcurrentException(e); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new ConcurrentException("Interrupted", e); + } catch (ExecutionException e) { + throw new ConcurrentException(e.getMessage(), e.getCause()); } }