Tomas Bisciak
Tomas Bisciak

Reputation: 2851

ShutdownHook code not being executed on System.exit(0)

I have Shutdown hook that i attach to runtime

Runtime.getRuntime().addShutdownHook(new ShutDownHook(false));

Here is a shutDownHook Class

public class ShutDownHook extends Thread {

    private final boolean interupt;

    public ShutDownHook(boolean interupt) {
        this.interupt = interupt;
    }

    @Override
    public void run() {

        if (interupt) {
            return;
        }
        System.out.println("ShutdownHook Execution");
        DbUtil.insertIntoDailyStats(MainDataModel.downloadedBytesSessionProperty().getValue());
        MainDataModel.getInstance().loginProfile.getPreferences().putLong(
                Info.PreferenceData.PREF_USER_DAILY_STAT_DOWNBYTE, MainDataModel.downloadedBytesTodayProperty().get());

         System.out.println("ShutdownHook Execution finished");

    }

}

And i close my application from System tray icon with a method

 exit.addActionListener((ActionEvent e) -> {
            try {
                GlobalScreen.unregisterNativeHook();
                System.exit(0);
            } catch (NativeHookException ex) {
                ex.printStackTrace();
            }
        });

Application closes bud hook execution didnt went thru, any idea why?

I know there are cases when ShutdownHook doesnt execute bud im closing my application with System.exit(0); that shoud be all safe and sound?

Upvotes: 1

Views: 823

Answers (1)

Tomas Bisciak
Tomas Bisciak

Reputation: 2851

Ok i found a problem i had multiple ShutDownHooks hooked in for some reason this one didnt executed , i removed all beside one and now everything works ok.Maybe too much of a load.

Works flawlessly , also if you use netbeans dont use RED TERMINATION BUTTON , - just a note.That way you never get execution of SDH.

Upvotes: 1

Related Questions