如何优雅地退出java应用程序?

如何优雅地退出java应用程序?,java,java-8,interrupt-handling,Java,Java 8,Interrupt Handling,我有一个java应用程序,它使用Executors.newSingleThreadScheduledExecutor定期运行一些函数 在我一直等待的主要功能中,使用: Thread.currentThread().join(); java应用程序能否识别它正在被关闭,即通过Ctrl-C、Ctrl-D信号关闭,特别是通过运行计划任务的线程关闭 我们的想法是优雅地关闭应用程序。要优雅地关闭Executor服务,您需要按照以下步骤进行 executorService.Shutdownow; 执行人服

我有一个java应用程序,它使用Executors.newSingleThreadScheduledExecutor定期运行一些函数

在我一直等待的主要功能中,使用:

Thread.currentThread().join();
java应用程序能否识别它正在被关闭,即通过Ctrl-C、Ctrl-D信号关闭,特别是通过运行计划任务的线程关闭


我们的想法是优雅地关闭应用程序。

要优雅地关闭Executor服务,您需要按照以下步骤进行

executorService.Shutdownow; 执行人服务。等待终止; 1执行器将尝试中断其管理的线程,并拒绝提交所有新任务

请等待现有任务终止 下面是一个优美的执行器关闭示例

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();
}
请查找完整详细的anwser


希望获得帮助

正常关闭Executor服务,您需要按照以下步骤进行操作

executorService.Shutdownow; 执行人服务。等待终止; 1执行器将尝试中断其管理的线程,并拒绝提交所有新任务

请等待现有任务终止 下面是一个优美的执行器关闭示例

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();
}
请查找完整详细的anwser

希望有帮助

添加一个信号处理程序。在处理程序中,使其停止生成周期线程,并加入或强制杀死现有线程。

添加一个来处理信号。在处理程序中,使其停止生成句点线程,并加入或强制杀死现有线程。

向Java运行时注册一个关闭挂钩。JVM的关闭将在注册的线程上得到通知。以下是一个例子:

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();
    }
}
向Java运行时注册一个关闭钩子。JVM的关闭将在注册的线程上得到通知。以下是一个例子:

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();
    }
}

问题是如何对Ctrl-C信号做出反应,还是如何等待执行器完成?或者问题完全不同了?@Progman,主要是关于如何对Ctrl-C做出反应,不过我也想知道Thread.currentThread.join是否可以。你想知道哪种“优雅关闭”的可能重复?JVM无论如何都会关闭,无论您是否调用executor服务上的shutdown,都没有任何效果,因为在您的调用生效之前线程仍然会被终止。问题是如何对Ctrl-C信号做出反应,还是如何等待executor完成?或者问题完全不同了?@Progman,主要是关于如何对Ctrl-C做出反应,不过我也想知道Thread.currentThread.join是否可以。你想知道哪种“优雅关闭”的可能重复?JVM无论如何都会关闭,无论您是否调用executor服务上的shutdown,都没有任何效果,因为在您的调用生效之前,线程仍然被终止。CodeIsLife,谢谢,但我无法主动调用pool.shutdown,因为主线程正在加入。然而,应用程序可能会被SIGINTCodeIsLife关闭,谢谢,但我不能主动调用pool.shutdown,因为主线程正在加入。然而,应用程序可以通过SIGINT关闭