26

I'm having a problem with closing my application because some threads are still running after I close the application. Somebody can help me with some method to stop all Threads being executed in background before killing the main thread???

[EDITED]

With my questions about javafx I have noticed that many newer developers are facing problem managing Threads. I would like to share what I have done to simplify my life about managing threads on javafx. I've created an AsyncTask class based on AsyncTask from Android that basically do the same of Android's in a humble but effective way. You can find more information about it on Github project

Victor Laerte
  • 6,446
  • 13
  • 53
  • 102

5 Answers5

40

You have three options here - the easiest is to simply create your Threads as deamons, which means that when your main program ends all deamon threads will terminate too.

Thread thread = new Thread();
thread.setDaemon(true);

Thats easiest, but the downside is that you wont get a graceful shutdown (ie the threads will stop, you wont get a chance to peform resource management etc, which may or may not be a problem for you).

The alternative is to keep a hold on the spawned threads and when your program receives the signal to close you iterate over the threads and pass in a signal of some sort to signa that they too should terminate

volatile boolean shutdown = false;

public void shutdown() {
   shutdown = true;
}

public void run() {
    while(!shutdown) { 
        ... do your work here
    }
    ... perform any cleanup work here

(Note: ignores any exception handling for clarity)

The last option is to use the Executor framework in java.util.concurrent

ExecutorService executorService = Executors.newFixedThreadPool(10);
... assign/invoke tasks etc

... at some point later your program is told to shutdown 
... shutdown in executor too 
executorService.shutdown();
executorService.awaitTermination(10, TimeUnit.SECONDS); // wait for 10s in this case
executorService.shutdownNow();
Sean Landsman
  • 7,033
  • 2
  • 29
  • 33
  • Note that the "at some point later your program is told to shutdown" point for a JavaFX program is usually in the body of an overridden [Application.stop()](https://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#stop--) method. Also note that the [Executors documentation usage examples](https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html) provides a nice template for coding graceful shutdown logic for an executor service. – jewelsea Apr 25 '17 at 00:29
  • Also, you can run the shutdown in a [JavaFX Task](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Task.html) if necessary if it may take a long time (e.g. so the app screen would not be frozen on shutdown for what might be a 10 second, or whatever you configure, termination wait). – jewelsea Apr 25 '17 at 00:34
39

Better way to fix this is add the EventHandler on Close Request:

@Override
public void start(Stage primaryStage) {
    primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
       @Override
       public void handle(WindowEvent e) {
          Platform.exit();
          System.exit(0);
       }
    });
}
Mirimas
  • 735
  • 10
  • 15
  • This works on Windows, but not on Linux (Ubuntu, Java version "1.7.0_67"). I get the error "is not responding. You may choose to wait a short while for it to continue or force the application to quit entirely". – stefan.m Jan 03 '17 at 14:54
  • 1
    This assumes you want the entire application to close rather than just the processes started from a particular window. – Legato Apr 24 '17 at 18:41
5

Override your Application Class

//System.exit(0) This will close all timers and threads inside the Jar application...
@Override
public void stop() throws Exception {
    super.stop(); //To change body of generated methods, choose Tools | Templates.
    System.exit(0);
}
guerino
  • 51
  • 1
  • 1
4

Executors from the java.util.concurrent package are the way to go. Explicitly:

ExecutorService executorService = Executors.newCachedThreadPool(new ThreadFactory() {
     @Override
     public Thread newThread(Runnable runnable) {
          Thread thread = Executors.defaultThreadFactory().newThread(runnable);
          thread.setDaemon(true);
          return thread;
    }
});

Java 8 version with fixed ScheduledExecutorService

ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1, r -> {
    Thread thread = Executors.defaultThreadFactory().newThread(r);
    thread.setDaemon(true);
    return thread;
});
Legato
  • 609
  • 17
  • 24
2

The method Platform.exit() belongs to the JavaFX context. When you call Platform.exit(), the method javafx.application.Application#stop() is called before the context terminates. Put inside the stop() method everything that needs to be executed before the JavaFX context terminates.

With the System.exit(0) method, the application terminate abruptly. This method is not secure because if at the moment you call System.exit(0) and a Job is still running, maybe executing a write in the database, the application will not wait the Job fihish resulting in a corrupted database.

I have an application running JavaFX with SpringBoot and a thread pool. That is how I handle it.

//...
import javafx.application.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

@SpringBootApplication
public class Main extends Application {

    ConfigurableApplicationContext context;

    ScheduledExecutorService scheduledExecutorService;


    @Override
    public void init() {

        this.context = SpringApplication.run(getClass());

        this.context.getAutowireCapableBeanFactory().autowireBean(this);

        this.scheduledExecutorService = Executors.newScheduledThreadPool(10);

    }

    @Override
    public void stop() throws Exception {

        super.stop();

        this.context.close();

        this.scheduledExecutorService.shutdownNow();

    }


    // ...

}