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

查看:77
本文介绍了在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进行清理?

来自doc的报价:


当应用程序停止时调用此方法,并提供
方便的位置来准备应用程序退出并销毁
资源。
Application类提供的此方法的实现不执行任何操作。

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的资源?到目前为止,我根本没有看到使用shutdownhook over stop()的原因。

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()退出应用程序时才会调用/ code>(或者当平台关闭最后一个窗口时.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.

在FX应用程序线程上执行stop() ,因此访问UI元素是安全的(例如,显示保存未保存的更改对话框等)。关闭挂钩在后台线程中运行,因此无法访问UI元素(事实上,FX Toolkit可能很久就会停止在该阶段运行)。

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并选择退出将执行停止()方法和关闭钩子。

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 终止进程,只执行shutdown hook。通过按挂起按钮然后右键单击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 id kill -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天全站免登陆