=========================================================================
import sun.misc.Signal;
import sun.misc.SignalHandler;
import java.util.Random;
public class SignalTerm {
- public static void main(String[] args) {
- try{
- System.out.println("System start.");
DiagSignalHandler.install("INT");
ThreadGroup tg = new ThreadGroup("AAA");
MyThread mt[] = new MyThread[10];
for(int i=0; i < mt.length; i++){
- mt[i] = new MyThread(tg,i);
mt[i].start();
for(int i=0; i < mt.length; i++){
- mt[i].join();
System.out.println("System exit."); - mt[i] = new MyThread(tg,i);
} catch(Exception e){
- System.out.println("exception: " + e.getMessage());
e.printStackTrace();
- System.out.println("System start.");
- try{
class MyThread extends Thread {
- private int threadcnt;
public MyThread(ThreadGroup tg, int n){
- super(tg, "My" + n);
threadcnt = n;
public void run(){
- try {
- System.out.println("In thread " + threadcnt);
while(true) sleep(1000);
} catch (InterruptedException e) {
- Random ran = new Random();
int rn = ran.nextInt(20);
System.out.println("Thread " + threadcnt + " is shuting down. Please wait " + rn + " seconds.");
try {
- sleep(rn* 1000);
} catch (InterruptedException e1) {}
System.out.println("Thread " + threadcnt + " exit.");
- System.out.println("In thread " + threadcnt);
- super(tg, "My" + n);
//Diagnostic Signal Handler class definition
class DiagSignalHandler implements SignalHandler {
- private SignalHandler oldHandler;
// Static method to install the signal handler
public static DiagSignalHandler install(String signalName) {
- Signal diagSignal = new Signal(signalName);
DiagSignalHandler diagHandler = new DiagSignalHandler();
diagHandler.oldHandler = Signal.handle(diagSignal,diagHandler);
return diagHandler;
// Signal handler method
public void handle(Signal sig) {
- System.out.println("Diagnostic Signal handler called for signal "+sig);
try {
- // Output information for each thread
Thread[] threadArray = new Thread[Thread.activeCount()];
int numThreads = Thread.enumerate(threadArray);
System.out.println("Current threads:");
for (int i=0; i < numThreads; i++) {
- System.out.println(" "+threadArray[i] + ", " + threadArray[i].getThreadGroup().getName());
if(threadArray[i].getThreadGroup().getName().equalsIgnoreCase("AAA")){
- threadArray[i].interrupt();
for (int i=0; i < numThreads; i++) {
- if((threadArray[i] != null) && (threadArray[i].getThreadGroup() != null) &&
(threadArray[i].getThreadGroup().getName().equalsIgnoreCase("AAA"))){
- threadArray[i].join();
// Chain back to previous handler, if one exists
if ( oldHandler != SIG_DFL && oldHandler != SIG_IGN ) {
- oldHandler.handle(sig);
- System.out.println(" "+threadArray[i] + ", " + threadArray[i].getThreadGroup().getName());
} catch (Exception e) {
- System.out.println("Signal handler failed, reason "+e);
e.printStackTrace();
- // Output information for each thread
- Signal diagSignal = new Signal(signalName);
=========================================================================
Heinz[2] mentioned the signal lists in different OSs:
Windows: ABRT, FPE, ILL, INT, SEGV, TERM
Solaris: ABRT, ALRM, BUS, CHLD, CONT, EMT, FPE, HUP, ILL, INT, IO, KILL, PIPE, POLL, PROF, PWR, QUIT, SEGV, STOP, SYS, TERM, TRAP, TSTP TTIN, TTOU, URG, USR1, USR2, VTALRM, WINCH, XCPU, XFSZ
References:
Chris White, Revelations on Java signal handling and termination, http://www.ibm.com/developerworks/ibm/library/i-signalhandling/
Dr. Heinz M. Kabutz, Switching off OS signals at runtime,
http://www.roseindia.net/javatutorials/
switching_off_os_signals_at_runtime.shtml