- java.lang.Object
- sun.misc.Signal
public final classSignalextendsObject
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.handlefunction raises anIllegalArgumentExceptionif such an attempt is made. - When
Signal.handleis 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 priorityThread.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:
constructs a signal object corresponding tonew Signal("INT");SIGINT, which is typically produced when the user pressesCtrl-Cat the command line. TheSignalconstructor throwsIllegalArgumentExceptionwhen 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);- Since:
- 1.2
- See Also:
SignalHandler
- Java code cannot register a handler for signals that are already used by the Java VM implementation. The
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object other)Compares the equality of twoSignalobjects.StringgetName()Returns the signal name.intgetNumber()staticSignalHandlerhandle(Signal sig,SignalHandler handler)Registers a signal handler.inthashCode()Returns a hashcode for this Signal.static voidraise(Signal sig)Raises a signal in the current process.StringtoString()Returns a string representation of this signal.
Constructor Detail
Signal
public Signal(String name)
Constructs a signal from its name.- Parameters:
name- the name of the signal.- Throws:
IllegalArgumentException- unknown signal- See Also:
getName()
Method Detail
getNumber
public int getNumber()
getName
public String getName()
Returns the signal name.- Returns:
- the name of the signal.
- See Also:
Signal(String name)
equals
public boolean equals(Object other)
Compares the equality of twoSignalobjects.- Overrides:
equalsin classObject- Parameters:
other- the object to compare with.- Returns:
- whether two
Signalobjects are equal. - See Also:
Object.hashCode(),HashMap
hashCode
public int hashCode()
Returns a hashcode for this Signal.- Overrides:
hashCodein classObject- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object),System.identityHashCode(java.lang.Object)
toString
public String toString()
Returns a string representation of this signal. For example, "SIGINT" for an object constructed usingnew Signal ("INT").
handle
public static SignalHandler handle(Signal sig,SignalHandler handler) throwsIllegalArgumentException
Registers a signal handler.- Parameters:
sig- a signalhandler- the handler to be registered with the given signal.- Returns:
- the old handler
- Throws:
IllegalArgumentException- the signal is in use by the VM- See Also:
raise(Signal sig),SignalHandler,SignalHandler.SIG_DFL,SignalHandler.SIG_IGN
raise
public static void raise(Signal sig) throwsIllegalArgumentException
Raises a signal in the current process.- Parameters:
sig- a signal- Throws:
IllegalArgumentException- See Also:
handle(Signal sig, SignalHandler handler)