0

Does Shutdown Hooks run when a computer shut down? I saw a post about this from a while back, and it is from 2015... so it makes me think that this could have been fixed/changed since then. If not, how should I do this, without causing any issues?

EDIT: I have done some testing myself and found that it does not run on system shut down. Using this:

Runtime.getRuntime().addShutdownHook(new Thread() {
    public void run() {
        //some code here
    }
});

I am using this so I could be able to disable some token/key that was generated at the start of the program. If I cannot disable the token at the end of the program, things will get all messed up...

Thanks for any input given!

Community
  • 1
  • 1
Swedz
  • 89
  • 1
  • 2
  • 7
  • @Mike'Pomax'Kamermans Yeah I under stand that, I just didn't want to have to reboot my computer as I typically have a lot of tabs open and programs as well... I felt it would be much easier to just ask and see if anyone knew already rather than having to go through the struggle of having to reboot my entire computer just to try a simple bit of code. – Swedz Apr 09 '17 at 04:36
  • 2
    Down-voted because "This question does not show any research effort" *(quoting tooltip of down-vote button)* Laziness is not an excuse for lack of research. – Andreas Apr 09 '17 at 04:43
  • Beyond that: there are many versions of Java and Windows, so a single experiment would not provide a really complete answer. – GhostCat Apr 09 '17 at 04:43
  • @Swedz it *is* easier, but that's also why the stackoverflow policy is "put effort into a question". If you can't be bothered to put in the effort to try something, expecting SO to put in the effort to answer is a little strange =) Give the code a try, see what it does, if it works: hurray. If not: you have excellent data to improve your question with. – Mike 'Pomax' Kamermans Apr 09 '17 at 04:50
  • @Swedz that's a hook for JVM shutdown, not for computer/system shutdown, as the documentation clearly explains: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread) – Mike 'Pomax' Kamermans Apr 09 '17 at 04:51
  • @Mike'Pomax'Kamermans clearly, but my question also states "if not, how should I do this". I also felt that the JVM would be shut down when the machine shuts down ... – Swedz Apr 09 '17 at 04:54
  • a system shutdown definitely shuts down the JVM, but the system typically doesn't wait around for shutdown hooks to kick in when it's being told to shut down, because the system is shutting down and requests for new threads or processes by applications will get denied. Same reason you can't rely on `finally` clauses to ever kick in: if the system shuts down, you have no guarantee that any part of your code is going to be allowed to keep running. – Mike 'Pomax' Kamermans Apr 09 '17 at 05:03
  • @Mike'Pomax'Kamermans could a new thread with a repeating task that runs code when it is interrupted potentially work better than what I currently am using? – Swedz Apr 09 '17 at 05:05
  • No idea, you didn't describe what you actually need this for (and so it's impossible to say if there's a better way to do that than what you're currently thinking of). It might be a good idea to add that information to the post. – Mike 'Pomax' Kamermans Apr 09 '17 at 05:06
  • @Mike'Pomax'Kamermans added what I'm using this for – Swedz Apr 09 '17 at 05:15
  • @Swedz my windows 10 laptop reboots in less than 15 seconds. – Marichyasana Apr 09 '17 at 05:25
  • @Swedz it'll probably be better to rethink your strategy: is that key going to interfere with anything that isn't your own application? If so, use something else. If not, then rather than relying on your application shutting down properly (which you can *never* rely on), make the application do key administration on startup, so the old value(s) are removed before the current value(s) get set. – Mike 'Pomax' Kamermans Apr 09 '17 at 18:05
  • @Mike'Pomax'Kamermans I have decided off of using this shutdown hook and just creating a socket connection to the http page, and the page will send keep alive info to the database every 5 seconds and when it misses some amount of keep alive info it will delete the token, making it unusable. – Swedz Apr 09 '17 at 19:17
  • That doesn't sound like "working with a token/key pair" to me. Do what sounds best to you, but this question feels thoroughly [XY](https://meta.stackexchange.com/a/66378)'d to me at this point. – Mike 'Pomax' Kamermans Apr 09 '17 at 19:29

0 Answers0