本文主要包括Java如何处理信号,直接上代码。
1. 实现SignalHandler
package com.chzhao.SignalTest; import sun.misc.*; @SuppressWarnings("restriction") public class TestSignal implements SignalHandler { public void handle(Signal arg0) { System.out.println(arg0.getName() + "is recevied."); } }
2.主函数部分
package com.chzhao.SignalTest; import sun.misc.*; public class App { @SuppressWarnings("restriction") public static void main(String[] args) { TestSignal handler = new TestSignal(); Signal.handle(new Signal("TERM"), handler); Signal.handle(new Signal("INT"), handler); Signal.handle(new Signal("USR1"), handler); Signal.handle(new Signal("USR2"), handler); for (;;) { System.out.println("do something"); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
这里面注册了4个信号。
在Linux下支持的信号(具体信号kill -l命令查看):
SEGV, ILL, FPE, BUS, SYS, CPU, FSZ, ABRT, INT, TERM, HUP, USR1, USR2, QUIT, BREAK, TRAP, PIPE
在Windows下支持的信号:
SEGV, ILL, FPE, ABRT, INT, TERM, BREAK