通过 Shutdownhook 使用 JavaFX Application.stop() 方法 [英] Using JavaFX Application.stop() method over Shutdownhook

查看:30
本文介绍了通过 Shutdownhook 使用 JavaFX Application.stop() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我使用shutdownhook来清理,由于它并不总是保证shutdownhooks线程执行,我是否应该将此代码推送到每次关闭应用程序时执行的JavaFX应用程序线程(方法stop())?代码运行起来并不昂贵,它基本上只是在没有关闭的情况下关闭套接字,如果没有被杀死就杀死进程.

So im using shutdownhook to clean up, bud since its not always guaranteed that shutdownhooks thread executes , should i just push this code onto JavaFX Application Thread (method stop()) which executes every time i close my application? Code is not expensive to run its basically just close socket if not closed and kill processs if not killed.

使用 Application.stop() 通过 ShutdownHook 进行清理是个好习惯吗?

引自文档:

这个方法在应用程序应该停止时调用,并提供一个准备应用程序退出和销毁的便利位置资源.该方法的实现由应用程序类什么都不做.

This method is called when the application should stop, and provides a convenient place to prepare for application exit and destroy resources. The implementation of this method provided by the Application class does nothing.

注意:此方法在 JavaFX 应用程序线程上调用.

NOTE: This method is called on the JavaFX Application Thread.

此方法是否仅用于清理 UI 资源?到目前为止,我根本看不到在 stop() 上使用 shutdownhook 的原因.

Is this method only intended to clean up resources of UI? So far i dont see reason using shutdownhook over stop() at all.

推荐答案

stop() 只有在通过 Platform.exit() 退出应用程序时才会被调用(或者当最后一个窗口关闭时(如果 Platform.implicitExit 为真).除了通常的方式之外,如果 System.exit() 被调用,或者运行 JVM 的本机进程被中断(例如,在类似 *nix 的操作系统上按 ctrl-C),关闭钩子将执行退出 JavaFX 应用程序.

stop() will only be called if you exit the application via Platform.exit() (or when the last window is closed if Platform.implicitExit is true). Shutdown hooks will execute if System.exit() is called, or if the native process running the JVM is interrupted (e.g. ctrl-C on a *nix-like OS), in addition to the usual way of exiting a JavaFX Application.

请注意,在调用 Application.launch() 之前,在主线程中注册关闭钩子似乎很重要.

Note that it appears to be important to register the shutdown hook in the main thread, before calling Application.launch().

stop() 在 FX 应用线程上执行,因此访问 UI 元素是安全的(例如,显示保存未保存的更改"对话框等).关闭挂钩在后台线程中运行,因此无法访问 UI 元素(实际上,FX 工具包很可能早就停止运行到那个阶段).

stop() is executed on the FX Application Thread, so it is safe to access UI elements (e.g. to show "save unsaved changes" dialogs, etc). Shutdown hooks are run in a background thread, so cannot access UI elements (indeed the FX Toolkit will probably long have stopped running by that stage).

所以选择取决于用例.

为了更具体一点,这里有一个快速测试类:

To make this a little more concrete, here's a quick test class:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class ShutdownTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button platformExit = new Button("Platform Exit");
        platformExit.setOnAction(e -> Platform.exit());
        Button systemExit = new Button("System Exit");
        systemExit.setOnAction(e -> System.exit(0));
        Button hang = new Button("Hang");
        hang.setOnAction(e -> {while(true);});
        HBox root = new HBox(5, platformExit, systemExit, hang);
        root.setPadding(new Insets(20));
        root.setAlignment(Pos.CENTER);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();

    }

    @Override
    public void stop() {
        System.out.println("Stop");
    }

    public static void main(String[] args) {
        Runtime.getRuntime().addShutdownHook(new Thread(() -> System.out.println("Shutdown hook")));
        launch(args);
    }
}

我在 Mac OS X 上对此进行了测试.

I tested this on Mac OS X.

通过平台退出"按钮退出,通过关闭窗口,或通过右键单击 Dock 并选择退出"将同时执行 stop() 方法和关闭挂钩.

Exiting via the "Platform Exit" button, by closing the window, or by right-clicking on the Dock and choosing "Quit" will execute both the stop() method and the shutdown hook.

通过系统退出"按钮退出,通过强制进程从活动监视器"退出,或通过命令行中的kill id杀死进程,只会执行关闭钩子.按挂起"按钮挂起应用程序,然后右键单击 Dock 并选择强制退出"具有相同的结果.

Exiting by the "System Exit" button, by forcing the process to quit from "Activity Monitor", or by killing the process by kill id from the command line, will execute the shutdown hook only. Hanging the application by pressing the "Hang" button and then right-clicking on the Dock and choosing "Force Quit" has the same result.

通过向进程发送 SIGKILL 退出(命令行中的 kill -9 idkill -SIGKILL id)既不执行 stop() 方法和关闭钩子.

Exiting by sending a SIGKILL to the process (kill -9 id or kill -SIGKILL id from the command line) executes neither the stop() method nor the shutdown hook.

这篇关于通过 Shutdownhook 使用 JavaFX Application.stop() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆