JDK-8246587 : ForkJoin updates
  • Type: CSR
  • Component: core-libs
  • Sub-Component: java.util.concurrent
  • Priority: P3
  • Status: Closed
  • Resolution: Withdrawn
  • Submitted: 2020-06-04
  • Updated: 2020-12-04
  • Resolved: 2020-12-04
Related Reports
CSR :  
Description
Summary
-------

Add cancel-on-interrupt ForkJoinTask adaptor

Problem
-------

Some potential FJ usages require that tasks are cancelled when their threads are interrupted, which is not the default policy for adapting Callables.

Solution
--------

Introduce a method adaptInterruptible that supports this policy.
An overview of the solution. Alternative solutions may be discussed;


Specification
-------------
    /**
     * Returns a new {@code ForkJoinTask} that performs the {@code call}
     * method of the given {@code Callable} as its action, and returns
     * its 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 callable the callable action
     * @param <T> the type of the callable's result
     * @return the task
     *
     * @since 15
     */
    public static <T> ForkJoinTask<T> adaptInterruptible(Callable<? extends T> callable)


Comments
The idea of adapting a task to make it interruptible is valuable, but interruption invariably raises thorny issues. It should be split into an independent change. I keep hoping for a better design for "reliable" interruption. I'm reminded of 1980 BSD (un)reliable signals.
04-12-2020

There's also this addition to class javadoc: ``` * <p>By default, method {@link #cancel} ignores its {@code * mayInterruptIfRunning} argument, separating task cancellation from * the interruption status of threads running tasks. However, the * method is overridable to accommodate cases in which running tasks * must be cancelled using interrupts. This may arise when adapting * Callables that cannot check {@code isCancelled()} task status. * Tasks constructed with the {@link #adaptInterruptible} adaptor * track and interrupt the running thread upon {@code * cancel(true)}. Reliable usage requires awareness of potential * consequences: Method bodies should ignore stray interrupts to cope * with the inherent possibility that a late interrupt issued by * another thread after a given task has completed may (inadvertently) * interrupt some future task. Further, interruptible tasks should not * in general create subtasks, because an interrupt intended for a * given task may be consumed by one of its subtasks, or vice versa. ```
04-12-2020