[concurrency-interest] ForkJoinTask does not re-check task status entailing overhead

Doug Lea dl at cs.oswego.edu
Tue Nov 6 07:10:03 EST 2018


On 11/6/18 4:11 AM, RĂ©mi Barat via Concurrency-interest wrote:
> Hi,
> 
> In one of our test we create around 260,000 ForkJoinTasks from one
> parent task. The parent task sets a flag which causes every child task
> to cancel itself (using super.cancel).
> 
> Please find attached a class to reproduce the test. In Java 8, this test
> is instantaneous, whereas it takes around 25s on my machine with Java 11.
> ...

> The question is: is the check on the task status necessary, or did we do
> something wrong?

The check is not necessary assuming that exec() returns true if done
(either normally or exceptionally), which it is required to do. You
should change the line:

        @Override
        protected boolean exec() {
            if (!interrupted.get()) {
                executeTask();
                return true;
            } else {
                super.cancel(false);
                return true; // was false
            }
        }

(In jdk8, it happened to work OK even without doing this.)
You could also add a line in your checkInterrupt method:

        protected void checkInterruption() {
            if (interrupted.get()) {
                cancel(false);    // added
                throw new CancellationException();
            }
        }


-Doug




More information about the Concurrency-interest mailing list