0

When my program use addShutDownHook, it acted strangly?

qiuxiafei
  • 5,827
  • 5
  • 30
  • 43
  • 3
    I don't understand the connection between your title and your question. And your question isn't really one. Please give more details about your actual problem. – Mat May 04 '12 at 09:31
  • -1. There is no such thing as a signal handler in Java. Your question is therefore meaningless. – user207421 May 04 '12 at 12:21

3 Answers3

2

addShutDownHook just adds a new Runnable(thread) which will be run by jvm only when jvm shutdown sequence is initiated. Code in that executes like normal code, but only on the special case mentioned earlier. It is usually used for some cleanup or logging purposes

Please re-check the code written to make sure it is 'logically' aligned to what you expect.

signal handling in java (especially on windows, documentation is still not 100% clear ) ref

Community
  • 1
  • 1
Anish Dasappan
  • 415
  • 2
  • 9
0

Signal handler is triggered when a signal is sent. The signal doesn't have to shutdown the application and if the application is shutdown, no signal hanlder might be called.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

You can implement the logic in SignalHandler’s handle method in which you can deal with different signals coming from the current active process under different platforms(OS) like unix, ilnux and windows. The logic in SignalHandler can be like stdout the signal name and timestamp to log or shutdown the process when it receives some special signal type.

When we use addShutDownHook we just set the order of threads exit and the sequence of the threads exit will be triggered only when JVM starts shutdown.

We often use addShutDownHook this method in a thread to release resources or network connections, and SignalHandler && Signal often be used to monitor from where and what the server process received during this process active time.