Skip to content

Commit

Permalink
fix(CompletableFutureUtils): backport the JDK bugfix JDK-8303742
Browse files Browse the repository at this point in the history
…for `Java 9~20` ⏰

more info see

- the JDK bug issue: https://bugs.openjdk.org/browse/JDK-8303742
- PR review: openjdk/jdk#13059
- JDK bugfix commit: openjdk/jdk@ded6a81
  • Loading branch information
oldratlee committed Aug 22, 2024
1 parent 30b912f commit e53b92e
Showing 1 changed file with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3731,7 +3731,7 @@ public static <C extends CompletableFuture<?>> C cffuOrTimeout(
* <a href="https://bugs.openjdk.org/browse/JDK-8303742">issue JDK-8303742</a>,
* <a href="https://github.com/openjdk/jdk/pull/13059">PR review openjdk/jdk/13059</a>
* and <a href="https://github.com/openjdk/jdk/commit/ded6a8131970ac2f7ae59716769e6f6bae3b809a">JDK bugfix commit</a>.
* The cffu backport logic(for Java 8) has merged the fix of this JDK bug.
* The cffu backport logic(for Java 20-) has merged the fix of this JDK bug.
*
* @param timeout how long to wait before completing exceptionally with a TimeoutException, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the {@code timeout} parameter
Expand All @@ -3743,7 +3743,9 @@ public static <C extends CompletableFuture<?>> C orTimeout(C cfThis, long timeou
requireNonNull(cfThis, "cfThis is null");
requireNonNull(unit, "unit is null");
// NOTE: No need check minimal stage, since checked at cf.orTimeout() / cf.isDone()
if (IS_JAVA9_PLUS) {

// because of bug JDK-8303742, delegate to CompletableFuture.orTimeout() when Java 21+(not Java 9+)
if (IS_JAVA21_PLUS) {
cfThis.orTimeout(timeout, unit);
} else {
// below code is copied from CompletableFuture#orTimeout with small adoption
Expand Down Expand Up @@ -4530,6 +4532,8 @@ private static Executor _asyncPool0() {

private static final boolean IS_JAVA19_PLUS;

private static final boolean IS_JAVA21_PLUS;

static {
boolean b;

Expand Down Expand Up @@ -4560,6 +4564,15 @@ private static Executor _asyncPool0() {
b = false;
}
IS_JAVA19_PLUS = b;

try {
// `List.reversed` is the new method since java 21
new ArrayList<>().reversed();
b = true;
} catch (NoSuchMethodError e) {
b = false;
}
IS_JAVA21_PLUS = b;
}

private CompletableFutureUtils() {
Expand Down

0 comments on commit e53b92e

Please sign in to comment.