Skip to content

Commit

Permalink
refactor(CFU): use static initializer instead of one-shot internal …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
oldratlee committed Oct 10, 2024
1 parent 87f0060 commit 09c5004
Showing 1 changed file with 36 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4042,46 +4042,6 @@ public static <T> CompletableFuture<T>[] completableFutureListToArray(List<Compl
return ex.getCause();
}

// endregion
////////////////////////////////////////////////////////////////////////////////
// region# Internal helper fields and classes
////////////////////////////////////////////////////////////////////////////////

/**
* Null-checks user executor argument, and translates uses of
* commonPool to ASYNC_POOL in case parallelism disabled.
*/
@SuppressWarnings("resource")
static Executor screenExecutor(Executor e) {
if (!USE_COMMON_POOL && e == ForkJoinPool.commonPool())
return ASYNC_POOL;
return requireNonNull(e, "defaultExecutor is null");
}

private static final boolean USE_COMMON_POOL = ForkJoinPool.getCommonPoolParallelism() > 1;

/**
* Fallback if ForkJoinPool.commonPool() cannot support parallelism
*/
private static final class ThreadPerTaskExecutor implements Executor {
@Override
public void execute(Runnable r) {
new Thread(requireNonNull(r)).start();
}
}

/**
* Default executor of CompletableFuture(<strong>NOT</strong> including the customized subclasses
* of CompletableFuture) -- {@link ForkJoinPool#commonPool()} unless it cannot support parallelism.
*/
private static final Executor ASYNC_POOL = _asyncPool();

private static Executor _asyncPool() {
if (IS_JAVA9_PLUS) return completedFuture(null).defaultExecutor();
if (USE_COMMON_POOL) return ForkJoinPool.commonPool();
return new ThreadPerTaskExecutor();
}

// endregion
////////////////////////////////////////////////////////////////////////////////
// region# Internal Java version check logic for compatibility
Expand Down Expand Up @@ -4116,13 +4076,47 @@ private static boolean methodExists(Supplier<?> methodCallCheck) {
}
}

// endregion
////////////////////////////////////////////////////////////////////////////////
// region# Internal helpers
////////////////////////////////////////////////////////////////////////////////

/**
* <strong>CAUTION / Implementation Note:</strong> do NOT move this static field before {@link #IS_JAVA9_PLUS},
* otherwise will use uninitialized {@link #IS_JAVA9_PLUS}!
* Null-checks user executor argument, and translates uses of commonPool to ASYNC_POOL
* in case parallelism disabled. code is copied from {@link CompletableFuture#screenExecutor(Executor)}.
*/
@SuppressWarnings({"resource", "JavadocReference"})
static Executor screenExecutor(Executor e) {
if (!USE_COMMON_POOL && e == ForkJoinPool.commonPool())
return ASYNC_POOL;
return requireNonNull(e, "defaultExecutor is null");
}

/**
* Fallback if ForkJoinPool.commonPool() cannot support parallelism
*/
private static final class ThreadPerTaskExecutor implements Executor {
@Override
public void execute(Runnable r) {
new Thread(requireNonNull(r)).start();
}
}

private static final boolean USE_COMMON_POOL = ForkJoinPool.getCommonPoolParallelism() > 1;

/**
* Default executor of CompletableFuture(<strong>NOT</strong> including the customized subclasses
* of CompletableFuture) -- {@link ForkJoinPool#commonPool()} unless it cannot support parallelism.
*/
private static final Executor ASYNC_POOL;

private static final Class<?> MIN_STAGE_CLASS;

static {
if (IS_JAVA9_PLUS) ASYNC_POOL = completedFuture(null).defaultExecutor();
else if (USE_COMMON_POOL) ASYNC_POOL = ForkJoinPool.commonPool();
else ASYNC_POOL = new ThreadPerTaskExecutor();

if (!IS_JAVA9_PLUS) MIN_STAGE_CLASS = null;
else MIN_STAGE_CLASS = CompletableFuture.completedStage(null).getClass();
}
Expand Down

0 comments on commit 09c5004

Please sign in to comment.