public final class

Signal

extends Object
java.lang.Object
   ↳ sun.misc.Signal

Class Overview

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);
 

See Also

Summary

Public Constructors
Signal(String name)
Constructs a signal from its name.
Public Methods
boolean equals(Object other)
Compares the equality of two Signal objects.
String getName()
Returns the signal name.
int getNumber()
synchronized static SignalHandler handle(Signal sig, SignalHandler handler)
Registers a signal handler.
int hashCode()
Returns a hashcode for this Signal.
static void raise(Signal sig)
Raises a signal in the current process.
String toString()
Returns a string representation of this signal.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Signal (String name)

Constructs a signal from its name.

Parameters
name the name of the signal.
Throws
IllegalArgumentException unknown signal
See Also

Public Methods

public boolean equals (Object other)

Compares the equality of two Signal objects.

Parameters
other the object to compare with.
Returns
  • whether two Signal objects are equal.

public String getName ()

Returns the signal name.

Returns
  • the name of the signal.
See Also

public int getNumber ()

public static synchronized SignalHandler handle (Signal sig, SignalHandler handler)

Registers a signal handler.

Parameters
sig a signal
handler the handler to be registered with the given signal.
Throws
IllegalArgumentException the signal is in use by the VM

public int hashCode ()

Returns a hashcode for this Signal.

Returns
  • a hash code value for this object.

public static void raise (Signal sig)

Raises a signal in the current process.

Parameters
sig a signal

public String toString ()

Returns a string representation of this signal. For example, "SIGINT" for an object constructed using new Signal ("INT").

Returns
  • a string representation of the signal