By skiwi


2013-10-28 15:50:57 8 Comments

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.

2 comments

@libik 2013-10-28 15:55:54

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...

@Debojit Saikia 2013-10-28 16:26:47

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.

@skiwi 2013-10-28 16:32:37

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.

@Debojit Saikia 2013-10-28 16:52:08

you can use System.exit(0); in code to shutdown your program.

@skiwi 2013-10-28 17:07:29

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.

@Debojit Saikia 2013-10-29 02:59:01

when you type crtl+c on console, the shutdown hook will run. and if have a UI, you can use System.exit(0);.

Related Questions

Sponsored Content

58 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

28 Answered Questions

55 Answered Questions

[SOLVED] How to create a memory leak in Java

65 Answered Questions

[SOLVED] How do I generate random integers within a specific range in Java?

  • 2008-12-12 18:20:57
  • user42155
  • 3923754 View
  • 3385 Score
  • 65 Answer
  • Tags:   java random integer

86 Answered Questions

[SOLVED] Is Java "pass-by-reference" or "pass-by-value"?

42 Answered Questions

[SOLVED] How do I convert a String to an int in Java?

47 Answered Questions

[SOLVED] Does a finally block always get executed in Java?

41 Answered Questions

[SOLVED] How do I efficiently iterate over each entry in a Java Map?

28 Answered Questions

[SOLVED] How do I determine whether an array contains a particular value in Java?

  • 2009-07-15 00:03:21
  • Mike Sickler
  • 1897091 View
  • 2201 Score
  • 28 Answer
  • Tags:   java arrays

32 Answered Questions

[SOLVED] When to use LinkedList over ArrayList in Java?

Sponsored Content