1

I have a java application which runs some function periodically using Executors.newSingleThreadScheduledExecutor

In the main() function I'm waiting forever using:

Thread.currentThread().join();

Can the java application identify that it's being closed (i.e. by Ctrl-C, Ctrl-D signals), particularly, the thread running the scheduled task?

The idea is to gracefully close the application.

uneq95
  • 2,158
  • 2
  • 17
  • 28
IsaacLevon
  • 2,260
  • 4
  • 41
  • 83
  • Is the question how to react on Ctrl-C signals or how to wait for the executor to finish? Or is the question completely different? – Progman Jan 27 '19 at 17:36
  • @Progman, it's mainly for how to react on Ctrl-C though I'd like to know also if `Thread.currentThread().join()` is ok. – IsaacLevon Jan 27 '19 at 17:59
  • Possible duplicate of [How to gracefully handle the SIGKILL signal in Java](https://stackoverflow.com/questions/2541597/how-to-gracefully-handle-the-sigkill-signal-in-java) – Progman Jan 27 '19 at 18:01
  • Which kind of “gracefully closing” do you have in mind? The JVM shuts down anyway and whether you call `shutdown` on the executor service or not, has no effect, as the threads are still killed before your call could have an effect. – Holger Jan 28 '19 at 09:20

3 Answers3

3

Register a shutdown hook with the Java Runtime. The shutdown of JVM will be notified on the registered thread. Below is an example:

public class Main {

    public static void main(String[] args) {

        Runtime.getRuntime().addShutdownHook(new ShutDownHookThread());

        while (true) {

        }

    }

}

class ShutDownHookThread extends Thread {
    @Override
    public void run() {
       // ***write your code here to handle any shutdown request
        System.out.println("Shut Down Hook Called");
        super.run();
    }
}
Amit Bera
  • 7,075
  • 1
  • 19
  • 42
1

to shutdown gracefully the Executor Service you need to proceed as following

  1. executorService.shutdownNow();
  2. executorService.awaitTermination();

1 the executor will try to interrupt the threads that it manages, and refuses all new tasks from being submitted.

  1. Wait a while for existing tasks to terminate

below an example of graceful Executor shutdown

pool.shutdown(); // Disable new tasks from being submitted
try {
    // Wait a while for existing tasks to terminate
    if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
        pool.shutdownNow(); // Cancel currently executing tasks
        // Wait a while for tasks to respond to being cancelled
        if (!pool.awaitTermination(60, TimeUnit.SECONDS))
            System.err.println("Pool did not terminate");
    }
} catch (InterruptedException ie) {
    // (Re-)Cancel if current thread also interrupted
    pool.shutdownNow();
    // Preserve interrupt status
    Thread.currentThread().interrupt();
}

please find here a complete detailed anwser

hope help

CodeIsLife
  • 1,205
  • 8
  • 14
  • CodeIsLife, thanks but I can''t actively call `pool.shutdown()` as the main thread is on `join()`. Yet, the application could be closed by a `SIGINT` – IsaacLevon Jan 27 '19 at 17:54
1

Add a shutdown hook to handle signals. In the handler, make it stop spawning the period thread, and join or force kill the existing thread.

viz
  • 1,247
  • 16
  • 27