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

ScheduledFuture.cancel( true ) does not interrupt a waiting() thread.

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      Win 10
      JRE 9.0.4

      A DESCRIPTION OF THE PROBLEM :
      Per Oracle Java documentation, the cancel method of the ScheduledFuture object returned by the schedule() method of the ScheduledThreadPoolExecutor class is supposed to interrupt running tasks when its mayInterruptIfRunning argument is true. It does not interrupt threads as expected.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      schedule a runnable to run, try to call cancel( true ) on the ScheduledFuture to interrupt it while it is in wait(), notice it does not get interrupted.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I'm about to start waiting.
      isDone: false
      cancellation successful: true
      I've been interrupted
      isDone: true

      ACTUAL -
      I'm about to start waiting.
      isDone: true
      cancellation successful: false
      isDone: true


      ---------- BEGIN SOURCE ----------
      import java.util.concurrent.ScheduledExecutorService ;
      import java.util.concurrent.ScheduledFuture ;
      import java.util.concurrent.ScheduledThreadPoolExecutor ;
      import java.util.concurrent.TimeUnit ;

      public class LocalTestClass
      {
      public static void main( String[] args )
      {
      ScheduledExecutorService schedulingShitBox = new ScheduledThreadPoolExecutor( 1 ) ;

      Runnable dumbAssRunnable = new Runnable()
      {
      public void run()
      {
      try
      {
      System.out.println( "I'm about to start waiting." ) ;
      wait() ;
      }
      catch ( InterruptedException theException )
      {
      System.out.println( "I've been interrupted" ) ;
      }
      }
      } ;

      ScheduledFuture theScheduledShit = schedulingShitBox.schedule( dumbAssRunnable,
      0,
      TimeUnit.MILLISECONDS ) ;

      // Wait 5 seconds to make sure the dumbAssRunnable is started and waiting...
      try { Thread.sleep( 5000 ) ; } catch ( Exception theException ){ /* do nothing */ }

      System.out.println( "isDone: " + theScheduledShit.isDone() ) ;

      System.out.println( "cancellation successful: " + theScheduledShit.cancel( true ) ) ;

      System.out.println( "isDone: " + theScheduledShit.isDone() ) ;

      schedulingShitBox.shutdownNow() ;
      }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      kill self

      FREQUENCY : always


            dholmes David Holmes
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: