Can I Register a Signal Handler in Java?
Um... Kind of!
There is an undocumented and unsupported interface called sun.misc.Signal present in most Java implementations. For those of you who have downloaded the JDK, the files live in /src/share/classes/sun/misc.
If you import sun.misc.Signal and sun.misc.SignalHandler, you can define a signal handler like this:
// Handles SIGHUP
Signal.handle(new Signal("HUP"), new SignalHandler() {
// Signal handler method
public void handle(Signal signal) {
System.out.println("Got signal" + signal);
}
});
You can raise a signal like this:
Signal.raise(new Signal("HUP"));
You can even register a native signal handler with the Signal.handle0 method (I'll leave that as an exercise for the reader).
Having said that, none of this helps us set timers or do profiling, really. It's just an interesting aside.

3 comments:
so true...
so very true...
so very very true...
Post a Comment