JavaFX app that uses Executor hangs on quit











up vote
1
down vote

favorite












I'm trying to make a clone of Android's AsyncTask to use in a JavaFX app. Here's the code I've come up with:



import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

abstract public class AsyncTask<Param, Result>
{
private Param param;
private static Executor executor;

public AsyncTask()
{
if (executor == null)
executor = Executors.newSingleThreadExecutor();
}

protected void onPreExecute()
{

}

protected Result doInBackground(Param param)
{
return null;
}

protected void onPostExecute(Result result)
{

}

final public void execute(Param param)
{
this.param = param;

onPreExecute();

Task t = new Task();
executor.execute(t);
}

private class Task implements Runnable
{
public void run()
{
Result result = doInBackground(param);
onPostExecute(result);
}
}
}


I can use this class in my JavaFX app and it works fine except for one thing: when I close the main window, the JVM hangs rather than exiting cleanly. I have to force-quit the app.



I think the problem is related to the Executor. Because I don't issue a shutdown(), the Executor hangs waiting for more tasks to execute. Because AsyncTask is a wrapper for Java's Executor, the class that extends AsyncTask won't have direct access to the Executor and therefore can't issue a shutdown(). How can I do an orderly shutdown of the Executor?










share|improve this question


























    up vote
    1
    down vote

    favorite












    I'm trying to make a clone of Android's AsyncTask to use in a JavaFX app. Here's the code I've come up with:



    import java.util.concurrent.Executor;
    import java.util.concurrent.Executors;

    abstract public class AsyncTask<Param, Result>
    {
    private Param param;
    private static Executor executor;

    public AsyncTask()
    {
    if (executor == null)
    executor = Executors.newSingleThreadExecutor();
    }

    protected void onPreExecute()
    {

    }

    protected Result doInBackground(Param param)
    {
    return null;
    }

    protected void onPostExecute(Result result)
    {

    }

    final public void execute(Param param)
    {
    this.param = param;

    onPreExecute();

    Task t = new Task();
    executor.execute(t);
    }

    private class Task implements Runnable
    {
    public void run()
    {
    Result result = doInBackground(param);
    onPostExecute(result);
    }
    }
    }


    I can use this class in my JavaFX app and it works fine except for one thing: when I close the main window, the JVM hangs rather than exiting cleanly. I have to force-quit the app.



    I think the problem is related to the Executor. Because I don't issue a shutdown(), the Executor hangs waiting for more tasks to execute. Because AsyncTask is a wrapper for Java's Executor, the class that extends AsyncTask won't have direct access to the Executor and therefore can't issue a shutdown(). How can I do an orderly shutdown of the Executor?










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm trying to make a clone of Android's AsyncTask to use in a JavaFX app. Here's the code I've come up with:



      import java.util.concurrent.Executor;
      import java.util.concurrent.Executors;

      abstract public class AsyncTask<Param, Result>
      {
      private Param param;
      private static Executor executor;

      public AsyncTask()
      {
      if (executor == null)
      executor = Executors.newSingleThreadExecutor();
      }

      protected void onPreExecute()
      {

      }

      protected Result doInBackground(Param param)
      {
      return null;
      }

      protected void onPostExecute(Result result)
      {

      }

      final public void execute(Param param)
      {
      this.param = param;

      onPreExecute();

      Task t = new Task();
      executor.execute(t);
      }

      private class Task implements Runnable
      {
      public void run()
      {
      Result result = doInBackground(param);
      onPostExecute(result);
      }
      }
      }


      I can use this class in my JavaFX app and it works fine except for one thing: when I close the main window, the JVM hangs rather than exiting cleanly. I have to force-quit the app.



      I think the problem is related to the Executor. Because I don't issue a shutdown(), the Executor hangs waiting for more tasks to execute. Because AsyncTask is a wrapper for Java's Executor, the class that extends AsyncTask won't have direct access to the Executor and therefore can't issue a shutdown(). How can I do an orderly shutdown of the Executor?










      share|improve this question













      I'm trying to make a clone of Android's AsyncTask to use in a JavaFX app. Here's the code I've come up with:



      import java.util.concurrent.Executor;
      import java.util.concurrent.Executors;

      abstract public class AsyncTask<Param, Result>
      {
      private Param param;
      private static Executor executor;

      public AsyncTask()
      {
      if (executor == null)
      executor = Executors.newSingleThreadExecutor();
      }

      protected void onPreExecute()
      {

      }

      protected Result doInBackground(Param param)
      {
      return null;
      }

      protected void onPostExecute(Result result)
      {

      }

      final public void execute(Param param)
      {
      this.param = param;

      onPreExecute();

      Task t = new Task();
      executor.execute(t);
      }

      private class Task implements Runnable
      {
      public void run()
      {
      Result result = doInBackground(param);
      onPostExecute(result);
      }
      }
      }


      I can use this class in my JavaFX app and it works fine except for one thing: when I close the main window, the JVM hangs rather than exiting cleanly. I have to force-quit the app.



      I think the problem is related to the Executor. Because I don't issue a shutdown(), the Executor hangs waiting for more tasks to execute. Because AsyncTask is a wrapper for Java's Executor, the class that extends AsyncTask won't have direct access to the Executor and therefore can't issue a shutdown(). How can I do an orderly shutdown of the Executor?







      java javafx android-asynctask executorservice java.util.concurrent






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 9:11









      Barry Brown

      13.2k116098




      13.2k116098
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You either need to shutdown the executor from the Application.stop method or you make sure the Executor's thread won't prevent the JVM from shutting down by using a daemon thread:



          if (executor == null) {
          executor = Executors.newSingleThreadExecutor(r -> {
          Thread t = new Thread(r);
          t.setDaemon(true);
          return t;
          });
          }





          share|improve this answer





















          • I used the setDaemon technique. Worked like a charm! Thanks a bunch!
            – Barry Brown
            Nov 7 at 17:34













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186395%2fjavafx-app-that-uses-executor-hangs-on-quit%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          4
          down vote



          accepted










          You either need to shutdown the executor from the Application.stop method or you make sure the Executor's thread won't prevent the JVM from shutting down by using a daemon thread:



          if (executor == null) {
          executor = Executors.newSingleThreadExecutor(r -> {
          Thread t = new Thread(r);
          t.setDaemon(true);
          return t;
          });
          }





          share|improve this answer





















          • I used the setDaemon technique. Worked like a charm! Thanks a bunch!
            – Barry Brown
            Nov 7 at 17:34

















          up vote
          4
          down vote



          accepted










          You either need to shutdown the executor from the Application.stop method or you make sure the Executor's thread won't prevent the JVM from shutting down by using a daemon thread:



          if (executor == null) {
          executor = Executors.newSingleThreadExecutor(r -> {
          Thread t = new Thread(r);
          t.setDaemon(true);
          return t;
          });
          }





          share|improve this answer





















          • I used the setDaemon technique. Worked like a charm! Thanks a bunch!
            – Barry Brown
            Nov 7 at 17:34















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          You either need to shutdown the executor from the Application.stop method or you make sure the Executor's thread won't prevent the JVM from shutting down by using a daemon thread:



          if (executor == null) {
          executor = Executors.newSingleThreadExecutor(r -> {
          Thread t = new Thread(r);
          t.setDaemon(true);
          return t;
          });
          }





          share|improve this answer












          You either need to shutdown the executor from the Application.stop method or you make sure the Executor's thread won't prevent the JVM from shutting down by using a daemon thread:



          if (executor == null) {
          executor = Executors.newSingleThreadExecutor(r -> {
          Thread t = new Thread(r);
          t.setDaemon(true);
          return t;
          });
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 7 at 9:35









          fabian

          48.7k114968




          48.7k114968












          • I used the setDaemon technique. Worked like a charm! Thanks a bunch!
            – Barry Brown
            Nov 7 at 17:34




















          • I used the setDaemon technique. Worked like a charm! Thanks a bunch!
            – Barry Brown
            Nov 7 at 17:34


















          I used the setDaemon technique. Worked like a charm! Thanks a bunch!
          – Barry Brown
          Nov 7 at 17:34






          I used the setDaemon technique. Worked like a charm! Thanks a bunch!
          – Barry Brown
          Nov 7 at 17:34




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186395%2fjavafx-app-that-uses-executor-hangs-on-quit%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Hercules Kyvelos

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud