0

I have a Java program that running on Windows 10 enviroment. When I shutdown Windows, I want my program to close gracefully using Java shutdown hook. The problem is, windows(or JVM) does not let my program to finish shutdown hook code block. Is there any way to delay shutting down windows until my shutdown hook is finished?

Note: I cannot make any shorter my hook code. It is handling some critical mechanishms.

Thanks in advance

yck
  • 33
  • 4
  • See the [Javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/Runtime.html#addShutdownHook-java.lang.Thread-). Shutdown hooks should not engage in blocking or long-running operations. – user207421 Jun 17 '22 at 08:10
  • 1
    How long would your shutdown hook require to complete if the app won't be killed? – SpaceTrucker Jun 17 '22 at 08:11
  • @user207421 Okay then, Is there a way to make windows wait to shutdown my program? I know sometimes windows gives a message like "Program x is preventing shutting down Windows" – yck Jun 17 '22 at 08:13
  • @SpaceTrucker It takes 2-3 seconds max but some situations it may take longer depens on system or internet connection – yck Jun 17 '22 at 08:14
  • Are your program running as a service? If I remember correctly, when services are shutdown they are not first getting a kind signal (like Ctrl-C) but are more brutally killed. – Just another Java programmer Jun 17 '22 at 08:14
  • @JustanotherJavaprogrammer No, not as a service just normal java app – yck Jun 17 '22 at 08:16
  • No there isn't. You have to comply with the stated requirement. Otherwise you cannot be sure your shutdown hook will run to completion (which in fact you cannot be sure of anyway). – user207421 Jun 17 '22 at 08:18

0 Answers0