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

SwingWorker calls 'done' before the 'doInBackground' is finished

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 21
    • 8u45, 8u60
    • client-libs
    • b12
    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_40"
      Java(TM) SE Runtime Environment (build 1.8.0_40-b25)
      Java HotSpot(TM) 64-Bit Server VM (build 25.40-b25, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      According to documentation of SwingWorker for method 'done':

      "Executed on the Event Dispatch Thread after the doInBackground method is finished."

      When cancelling a worker that needs to perform some cleanup the 'done' method is called on EDT before the worker finishes cleaning up.

      This behavior is not logical and not correct according to documentation.

      In the steps to reproduce i put a code listing that can be used to reproduce.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      - start a swing worker
      - cancel it
      - expect swing worker to do some cleanup


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Working...
      (...)
      Working...
      Got interrupted!
      Cleaning up
      Done cleaning
      Done

      ACTUAL -
      Working...
      (...)
      Working...
      Got interrupted!
      Cleaning up
      Done
      Done cleaning

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.SwingWorker;

      public class Main
      {
          public static void main(String[] args) throws InterruptedException
          {
              SwingWorker<String, String> worker = new SwingWorker<String, String>()
              {
                  @Override
                  protected String doInBackground() throws Exception
                  {
                      try
                      {
                          while (!Thread.currentThread().isInterrupted())
                          {
                              System.out.println("Working...");
                              Thread.sleep(1000);
                              
                          }
                      }
                      catch (InterruptedException ex)
                      {
                          System.out.println("Got interrupted!");
                      }
                      
                      try
                      {
                          System.out.println("Cleaning up");
                          Thread.sleep(10000);
                          System.out.println("Done cleaning");
                      }
                      catch (InterruptedException ex)
                      {
                          System.out.println("Got interrupted second time!");
                      }
                      
                      return null;
                  }
                  
                  @Override
                  protected void done()
                  {
                      System.out.println("Done");
                  }
              };
              
              worker.execute();
              Thread.sleep(10000);
              
              worker.cancel(true);
              Thread.sleep(20000);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Reimplement the 'FutureTask' class.

            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: