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

java/util/concurrent/ExecutorService/CloseTest.java failed with "InterruptedException: sleep interrupted"

    XMLWordPrintable

Details

    • CSR
    • Resolution: Approved
    • P3
    • 22
    • core-libs
    • None
    • source, binary, behavioral
    • low
    • Hide
      Existing sources that call invokeAll in contexts that otherwise don't deal with InterruptedExceptions will fail to compile. This is a rare situation, because invokeAll is rarely used with ForkJoinPools (instead using ForkJoinTask.invokeAll). A corpus search found only one case in which the source code would need to change to use the new Uninterruptible version. Probably there are a few others, but this impact is less serious than the current problem that most existing invokeAll calls unexpectedly ignore interrupts.
      Show
      Existing sources that call invokeAll in contexts that otherwise don't deal with InterruptedExceptions will fail to compile. This is a rare situation, because invokeAll is rarely used with ForkJoinPools (instead using ForkJoinTask.invokeAll). A corpus search found only one case in which the source code would need to change to use the new Uninterruptible version. Probably there are a few others, but this impact is less serious than the current problem that most existing invokeAll calls unexpectedly ignore interrupts.
    • Java API
    • SE

    Description

      Summary

      ForkJoinPool.invokeAll had incompatible Interruptiblity signature and behavior with its ExecutorService specs. This fixes the problem, while also adding an Uninterruptible version of the method for those who still need it.

      Problem

      Usages of ForkJoinPool.invokeAll, when accessed as an ExecutorService (e.g., via Executors.newWorkStealingPool) had behavior incompatible with specs, causing Interrupts to unexpectedly be ignored in nearly all usages.

      Solution

      Correct the signature and behavior of invokeAll, but also introduce method invokeAllUninterruptibly for use by any users who specifically want or need the prior version. Also add two overloads of ForkJoinTask.adaptInterruptible for uniformity.

      Specification

      The update invokeAll specs just inherit those of ExecutorService, so are left implicit.

      ForkJoinPool.invokeAllUninterruptibly:

      /**
       * Uninterrupible version of {@code InvokeAll}. Executes the given
       * tasks, returning a list of Futures holding their status and
       * results when all complete, ignoring interrupts.  {@link
       * Future#isDone} is {@code true} for each element of the returned
       * list.  Note that a <em>completed</em> task could have
       * terminated either normally or by throwing an exception.  The
       * results of this method are undefined if the given collection is
       * modified while this operation is in progress.
       *
       * @apiNote This method supports usages that previously relied on an
       * incompatible override of
       * {@link ExecutorService#invokeAll(java.util.Collection)}.
       *
       * @param tasks the collection of tasks
       * @param <T> the type of the values returned from the tasks
       * @return a list of Futures representing the tasks, in the same
       *         sequential order as produced by the iterator for the
       *         given task list, each of which has completed
       * @throws NullPointerException if tasks or any of its elements are {@code null}
       * @throws RejectedExecutionException if any task cannot be
       *         scheduled for execution
       * @since 22
       */
      public <T> List<Future<T>> invokeAllUninterruptibly(Collection<? extends Callable<T>> tasks)

      The two overloads for ForkJoinTask.adaptInterruptible include the same wording as the existing Callable version:

      /**
       * Returns a new {@code ForkJoinTask} that performs the {@code run}
       * method of the given {@code Runnable} as its action, and returns
       * the given result upon {@link #join}, translating any checked exceptions
       * encountered into {@code RuntimeException}.  Additionally,
       * invocations of {@code cancel} with {@code mayInterruptIfRunning
       * true} will attempt to interrupt the thread performing the task.
       *
       * @param runnable the runnable action
       * @param result the result upon completion
       * @param <T> the type of the result
       * @return the task
       *
       * @since 22
       */
      public static <T> ForkJoinTask<T> adaptInterruptible(Runnable runnable, T result)
      
      /**
       * Returns a new {@code ForkJoinTask} that performs the {@code
       * run} method of the given {@code Runnable} as its action, and
       * returns null upon {@link #join}, translating any checked
       * exceptions encountered into {@code RuntimeException}.
       * Additionally, invocations of {@code cancel} with {@code
       * mayInterruptIfRunning true} will attempt to interrupt the
       * thread performing the task.
       *
       * @param runnable the runnable action
       * @return the task
       *
       * @since 22
       */
      public static ForkJoinTask<?> adaptInterruptible(Runnable runnable)

      Attachments

        Issue Links

          Activity

            People

              dl Doug Lea
              dcubed Daniel Daugherty
              Alan Bateman
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: