Using JavaFX application. On the shutdown hook Stop() method

So I use shutdown hooks for cleaning, because it does not always guarantee the execution of the shutdown hooks thread. Should I push this code to the JavaFX application thread (method stop ()) that executes every time I close my application? If the code is not closed, it basically closes the socket, and if it is not killed, it is not expensive to kill the process

Use application Is it a good habit to stop () to clean up shutdown hook?

References from doc:

Is this method only used to clean up the resources of the UI? So far, I haven't seen the reason for using Shutdown hook over stop()

Solution

Only through platform Exit() will only call stop() when exiting the application (if platform.implicitexit is true, close the last window) If you call system Exit(), or the native process running the JVM is interrupted (for example, ctrl-c on an OS similar to * Nix). In addition to the common method of exiting a JavaFX application, the close hook will also be executed

Stop () is executed on the FX application thread, so it is safe to access UI elements (for example, display the save unsaved changes dialog box, etc.) The off hook runs in the background thread, so the UI elements cannot be accessed (in fact, the FX toolkit may stop running at this stage for a long time)

So the choice depends on the use case

To make this more specific, this is 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.H@R_195_2419@;
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);});
        H@R_195_2419@ root = new H@R_195_2419@(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);
    }
}

I tested this on Mac OS X

Exit through the "platform exit" button, close the window, or right-click the dock and select "exit" to execute the stop () method and close the hook at the same time

Exiting through the system exit button, forcing the process to exit from the activity monitor, or terminating the process through the kill ID in the command line will only execute the close hook Suspending an application by pressing the suspend button, then right clicking dock and selecting force exit has the same result

By sending sigkill to the process to exit (kill - 9 ID or kill - sigkill ID from the command line), neither the stop () method nor the shutdown hook is executed

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>