java 捕捉信号(linux下的kill,ctrl+c)

  • Post author:
  • Post category:java


代码

import sun.misc.Signal;
import sun.misc.SignalHandler;

@SuppressWarnings("restriction")
public class SignalHandlerExample {

 public static void main(String[] args) {
  System.out.println("Signal handling example.");
  SignalHandler handler = new MySignalHandler();
  // kill命令
  Signal termSignal = new Signal("TERM");
  Signal.handle(termSignal, handler);
  // ctrl+c命令
  Signal intSignal = new Signal("INT");
  Signal.handle(intSignal, handler);
  try {
   Thread.sleep(50000);
  } catch (InterruptedException e) {
   System.out.println("Interrupted: " + e.getMessage());
  }
 }
}

@SuppressWarnings("restriction")
class MySignalHandler implements SignalHandler {

 @Override
 public void handle(Signal signal) {
  System.out.println("Signal handler called for signal " + signal);
  try {
   System.out.println("Handling " + signal.getName());
  } catch (Exception e) {
   System.out.println("handle|Signal handler" + "failed, reason "
     + e.getMessage());
   e.printStackTrace();
  }
 }

}



版权声明:本文为zdq0394原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。