Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8350493

Improve performance of delayed task handling

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Unresolved
    • Icon: P4 P4
    • 25
    • core-libs
    • None
    • source
    • minimal
    • Hide
      * Very unlikely method name clashes with possible existing ForkJoinPool subclasses for 3 methods
      * The creation, type, and names of some internally generated threads may differ, which could impact tooling that hard-wired monitoring or control based on these
      * Further disclaimers and increasing discouragement about use of the java.util.concurrent.ForkJoinPool.common.parallelism system property, especially the ability to set to zero, which was originally done to allow JavaEE frameworks to prevent thread construction in parallelStreams operations in ways that might evade SecurityManagers, which are also now deprecated.
      Show
      * Very unlikely method name clashes with possible existing ForkJoinPool subclasses for 3 methods * The creation, type, and names of some internally generated threads may differ, which could impact tooling that hard-wired monitoring or control based on these * Further disclaimers and increasing discouragement about use of the java.util.concurrent.ForkJoinPool.common.parallelism system property, especially the ability to set to zero, which was originally done to allow JavaEE frameworks to prevent thread construction in parallelStreams operations in ways that might evade SecurityManagers, which are also now deprecated.

      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 int 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.

            dl Doug Lea
            dl Doug Lea
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated: