12

I have added the following code to my program:

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("exit");
    }
}){});

I however do not see the message. Additional information: I am running the program from inside the Netbeans IDE on Java 7.

EDIT: I forgot to add that there is a global Thread that keeps the program alive. I close it by pressing the [x] in Netbeans lower right corner.

4
  • Try sending a SIGHUP to the Java process (kill -1 <pid>) instead of clicking a UI button. en.wikipedia.org/wiki/Unix_signal#POSIX_signals Commented Oct 28, 2013 at 15:55
  • Your example works fine for me when I paste it into a main method. Commented Oct 28, 2013 at 15:59
  • 4
    Note that the {} is unnecessary. It actually creates an anonymous inner subclass of Thread. Commented Oct 28, 2013 at 16:01
  • The Runnable is not necessary. Instead, you can override the run method of the Thread and it will be functionally identical. Commented Oct 6, 2016 at 22:30

2 Answers 2

20

The JVM can shutdown in either an orderly or abrupt manner. A shutdown hook runs for an orderly shutdown: when the last normal thread terminates, someone calls System.exit or by other platform specific means (such as typing Ctrl-C).

Shutdown hooks will not run for an abrupt shutdown of the JVM. As you are pressing the [x] in Netbeans lower right corner, this will cause an abrupt shutdown of the JVM and this is why the shutdown hook was not started.

For example :

public class ShutdownHook {
public void attachShutDownHook() {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            System.out.println("exit");
        }
    });

}

public static void main(String[] args) {
    ShutdownHook sample = new ShutdownHook();
    sample.attachShutDownHook();
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

If you run the above code, and let the program complete normally, you will see exit printed on the console. But if you press [x] (within 3 secs) to close it abruptly, the shutdown hook will not run and there will not be any exit printed on the console.

Sign up to request clarification or add additional context in comments.

4 Comments

Okay, then I understand that. The current code (with a meaningful shutdown hook) is still worth keeping in. But is there also a way to do a proper shutdown with het Netbeans close? Also what would be a nice way of shutting down the program normally? I do not have access to System.In, as the console is being used for output constantly.
you can use System.exit(0); in code to shutdown your program.
The program is a program that must react when files are being added to a specific folder, so I'm afraid it needs to be running forever. Still need some way to properly shut it down in case of maintenance though.
when you type crtl+c on console, the shutdown hook will run. and if have a UI, you can use System.exit(0);.
2

I forgot to add that there is a global Thread that keeps the program alive. I close it by pressing the [x] in Netbeans lower right corner.

Well this is it, closing program by "x" in netbeans lower right corner is not regular shut down, it just breaks everything and shut it down.

ShutdownHook works only when the program regulary exits...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.