views:

69

answers:

3
  • What does this class exactly do?

  • What is a "termination-triggered shutdowns"?

  • Where have you used this in your program or what could be a good use case for this class?

+3  A: 

Taking your points one at a time:

  • What does this class do? It handles termination signals received from the Operating System.

  • What are "termination-triggered shutdowns"? A shut down of the JVM caused by the Operating System sending a signal to the Java process, e.g. when you shut down your computer.

  • How can it be used? It's not intended for use in your programs as it is package private and is used by the JVM itself to handle termination signals received from the Operating System.

BenM
+1: The proper use of `java.lang.Terminator` is only within the code of the `java.lang` package itself. Using it from anywhere else is not standards-conform and can break horribly in pretty much all situations.
Joachim Sauer
When does the OS send the termination signals?
kunjaan
If you run a java program from the shell then you can send the HUP signal by closing the shell process that launched the java process.The TERM signal will be sent to your java program if you start a shutdown of your computer while the java process is running. This will shortly be followed by a KILL signal if the termination isn't completed in time.You may find http://twit88.com/blog/2008/02/06/java-signal-handling/ interesting.
BenM
+2  A: 

It's package private - so you're not supposed to use it. Better look at http://java.sun.com/j2se/1.4.2/docs/guide/lang/hook-design.html if you want to execute code upon shutdown.

Anton
+1  A: 

It registers signal handlers for HUP (Hangup), INT (Interrupt) and TERM (Termination) (see List of Signals). It shuts down the application by calling System.exit() with a value of sig.getNumber() + 128, i.e. 1+128=129 (HUP), 2+128=130 (INT), 15+128=143 (TERM). So whenever you get one of this exit values, you know that the application was shut down by Terminator after receiving one of these signals from the OS. Note that KILL is handled by the OS directly.

This is a internal class, hence no usecase.

sfussenegger