sun.misc
public final class: Signal [javadoc |
source]
java.lang.Object
sun.misc.Signal
This class provides ANSI/ISO C signal support. A Java program can register
signal handlers for the current process. There are two restrictions:
-
Java code cannot register a handler for signals that are already used
by the Java VM implementation. The
Signal.handle
function raises an IllegalArgumentException if such an attempt
is made.
-
When
Signal.handle is called, the VM internally registers a
special C signal handler. There is no way to force the Java signal handler
to run synchronously before the C signal handler returns. Instead, when the
VM receives a signal, the special C signal handler creates a new thread
(at priority Thread.MAX_PRIORITY) to
run the registered Java signal handler. The C signal handler immediately
returns. Note that because the Java signal handler runs in a newly created
thread, it may not actually be executed until some time after the C signal
handler returns.
Signal objects are created based on their names. For example:
new Signal("INT");
constructs a signal object corresponding to
SIGINT, which is
typically produced when the user presses
Ctrl-C at the command line.
The
Signal constructor throws
IllegalArgumentException
when it is passed an unknown signal.
This is an example of how Java code handles SIGINT:
SignalHandler handler = new SignalHandler () {
public void handle(Signal sig) {
... // handle SIGINT
}
};
Signal.handle(new Signal("INT"), handler);
| Constructor: |
public Signal(String name) {
number = findSignal(name);
this.name = name;
if (number < 0) {
throw new IllegalArgumentException("Unknown signal: " + name);
}
}
Constructs a signal from its name. Parameters:
name - the name of the signal.
Throws:
IllegalArgumentException - unknown signal
Also see:
- sun.misc.Signal#getName()
- exception:
IllegalArgumentException - unknown signal
|
| Method from sun.misc.Signal Detail: |
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null || !(other instanceof Signal)) {
return false;
}
Signal other1 = (Signal)other;
return name.equals(other1.name) && (number == other1.number);
}
Compares the equality of two Signal objects. |
public String getName() {
return name;
}
|
public int getNumber() {
/* Returns the signal number */
return number;
}
|
public static synchronized SignalHandler handle(Signal sig,
SignalHandler handler) throws IllegalArgumentException {
long newH = (handler instanceof NativeSignalHandler) ?
((NativeSignalHandler)handler).getHandler() : 2;
long oldH = handle0(sig.number, newH);
if (oldH == -1) {
throw new IllegalArgumentException
("Signal already used by VM or OS: " + sig);
}
signals.put(new Integer(sig.number), sig);
synchronized (handlers) {
SignalHandler oldHandler = (SignalHandler)handlers.get(sig);
handlers.remove(sig);
if (newH == 2) {
handlers.put(sig, handler);
}
if (oldH == 0) {
return SignalHandler.SIG_DFL;
} else if (oldH == 1) {
return SignalHandler.SIG_IGN;
} else if (oldH == 2) {
return oldHandler;
} else {
return new NativeSignalHandler(oldH);
}
}
}
Registers a signal handler. |
public int hashCode() {
return number;
}
Returns a hashcode for this Signal. |
public static void raise(Signal sig) throws IllegalArgumentException {
if (handlers.get(sig) == null) {
throw new IllegalArgumentException("Unhandled signal: " + sig);
}
raise0(sig.number);
}
Raises a signal in the current process. |
public String toString() {
return "SIG" + name;
}
Returns a string representation of this signal. For example, "SIGINT"
for an object constructed using new Signal ("INT"). |