-
Type:
CSR
-
Resolution: Approved
-
Priority:
P4
-
Component/s: core-libs
-
None
-
source
-
minimal
-
-
SE
Summary
Improve the performance of delayed task handling with an update to java.util.concurrent.ForkJoinPool to implement ScheduledExecutorService.
Problem
In common network-related applications, delayed tasks to handle timeouts are scheduled but are usually cancelled. This may lead to excessive contention and avoidable locking, especially with virtual threads, in turn using ForkJoinPool. ScheduledThreadPoolExecutor is ill-suited for many (if not most) of its applications, and is a performance bottleneck with virtual threads and CompletableFuture.
Solution
Lazily connect support for ScheduledExecutorService to any ForkJoinPool instance, this includes the common pool.
In API terms java.util.concurrent.ForkJoinPool is updated to implement ScheduledExecutorService, thus adding 3 methods to its API:
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
Three other methods added: to submit a task with a timeout, a method to disable delayed tasks on shutdown, and a method to get an estimate of the number of delayed tasks that are schedueld.
public <V> ForkJoinTask<V> submitWithTimeout(Callable<V> callable,
long timeout, TimeUnit unit,
Consumer<? super ForkJoinTask<V>> timeoutAction)
public void cancelDelayedTasksOnShutdown()
public long getDelayedTaskCount()
CompletableFuture is updated for the corner case that the ForkJoinPool common pool is configured with zero parallelism. The async methods were to create a new Thread for each async task. This is changed, in a backward compatible way, to consistently use the common pool.
Finally, while in the area, the description of the corePoolSize parameter in the 10-arg ForkJoinPool constructor is changed to specify that the parameter is ignored. The parameter is already ignored, this change just aligns the spec with the long standing implementation.
Specification
A zip file is attached with the API diffs.
- csr of
-
JDK-8319447 Improve performance of delayed task handling
-
- Resolved
-
- duplicates
-
JDK-8213114 Java API doc needs update to specify that explicit usage of commonPool() for CompletableFuture may replace it
-
- Closed
-
-
JDK-8213115 ThreadPerTaskExecutor of CompletableFuture makes it unreliable
-
- Closed
-
- relates to
-
JDK-8362882 Update SubmissionPublisher() specification to reflect use of ForkJoinPool.asyncCommonPool()
-
- Resolved
-
-
JDK-8362881 Release Note: Updates to ForkJoinPool and CompletableFuture
-
- Resolved
-