strange behavior of future.cancel() with Executors.newWorkStealingPool()

Martin Buchholz martinrb at google.com
Mon Apr 27 19:51:43 UTC 2015


https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ForkJoinTask.html#cancel-boolean-

Parameters:
mayInterruptIfRunning - this value has no effect in the default
implementation because interrupts are not used to control cancellation.

Admittedly,
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Executors.html#newWorkStealingPool-int-
doesn't promise that it's using ForkJoinPool/ForkJoinTask.


On Mon, Apr 27, 2015 at 12:34 PM, Michał Słonina <michal.slonina at gmail.com>
wrote:

> Hello folks,
>
> I've noticed a strange behavior of future.cancel() when using work stealing
> thread pool.
>
> The JDK docs state that calling future.cancel(true) should result in task
> being interrupted.
> "Parameters: mayInterruptIfRunning - true if the thread executing this task
> should be interrupted..." [1]
>
> This works correctly with any executor other then work stealing pool. The
> behavior is consistent on JDK8 and JDK9.
> Can someone please enlighten me, did I miss something in the docs or is
> this a bug ?
>
> --- CUT HERE ---
>
> public class Test {
>     public static void main(String[] args) throws InterruptedException {
>         ExecutorService executor = args[0].equalsIgnoreCase("UGLY")
>                 ? Executors.newWorkStealingPool() :
> Executors.newFixedThreadPool(666);
>         Future<?> future = executor.submit(new Runnable() {
>             @Override
>             public void run() {
>                 try {
>                     System.out.println("STARTED");
>                     Thread.sleep(500);
>                     System.out.println("FINISHED. Is interrupted: " +
> Thread.interrupted());
>                 } catch (InterruptedException e) {
>                     e.printStackTrace();
>                 }
>             }
>         });
>         Thread.sleep(100);
>         boolean cancelResult = future.cancel(true);
>         System.out.println("Cancel result:" + cancelResult);
>         Thread.sleep(1000);
>         System.exit(0);
>     }
> }
>
> --- CUT HERE ---
>
> af1n executortest/src » java -cp . Test NICE
> STARTED
> Cancel result:true
> java.lang.InterruptedException: sleep interrupted
>         at java.lang.Thread.sleep(Native Method)
>         at Test$1.run(Test.java:21)
>         at
> java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
>         at java.util.concurrent.FutureTask.run(FutureTask.java:265)
>         at
>
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
>         at
>
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
>         at java.lang.Thread.run(Thread.java:745)
>
> af1n executortest/src » java -cp . Test UGLY
> STARTED
> Cancel result:true
> FINISHED. Is interrupted: false
>
> af1n executortest/src » java -version
> openjdk version "1.9.0_00"
> OpenJDK Runtime Environment (build 1.9.0_00-b60)
> OpenJDK 64-Bit Server VM (build 1.9.0_00-b60, mixed mode)
>
> af1n executortest/src » uname -a
> Linux af1n.2r-media.de 3.16.7-7-desktop #1 SMP PREEMPT Wed Dec 17 18:00:44
> UTC 2014 (762f27a) x86_64 x86_64 x86_64 GNU/Linux
>
> --
> [1]
>
> https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/Future.html#cancel-boolean-
>
> Kind Regards,
> Michal Slonina
>


More information about the jdk9-dev mailing list