2018年1月22日 | Leave a comment 问题: java 后台程序怎么处理 kill 解决: SignalHandler Runtime.getRuntime().addShutdownHook SignalHandler demo App.java package com.enjoylearn.testkill; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author sihai */ public class App { boolean isRunning = true; Thread t = null; public static void main(String[] argv) throws InterruptedException { App app = new App(); SignalHandlerHelper.registerHandler("TERM", app::stop); app.start(); app.t.join(); } public void start() { t = new Thread(() -> { while (isRunning) { System.out.println("dddddd"); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); } } }); t.start(); } public void stop() { System.out.println("stop"); isRunning = false; } } 12345678910111213141516171819202122232425262728293031323334353637383940 package com.enjoylearn.testkill; import java.util.logging.Level;import java.util.logging.Logger; /** * * @author sihai */public class App { boolean isRunning = true; Thread t = null; public static void main(String[] argv) throws InterruptedException { App app = new App(); SignalHandlerHelper.registerHandler("TERM", app::stop); app.start(); app.t.join(); } public void start() { t = new Thread(() -> { while (isRunning) { System.out.println("dddddd"); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex); } } }); t.start(); } public void stop() { System.out.println("stop"); isRunning = false; }} SignalHandlerHelper.java package com.enjoylearn.testkill; import java.util.Objects; import sun.misc.Signal; import sun.misc.SignalHandler; /** * * @author sihai */ public class SignalHandlerHelper implements SignalHandler { final static String NAME_TERM = "TERM"; private final Runnable handler; public static void registerHandler(String name, Runnable handler) { SignalHandlerHelper kh = new SignalHandlerHelper(name, handler); kh.register(); } private final String name; void register() { Signal s = new Signal(name); Signal.handle(s, this); } public SignalHandlerHelper(String name, Runnable handler) { this.name = name; this.handler = handler; } @Override public void handle(Signal signal) { if (Objects.equals(this.name, signal.getName())) { handler.run(); } } } 123456789101112131415161718192021222324252627282930313233343536373839 package com.enjoylearn.testkill; import java.util.Objects;import sun.misc.Signal;import sun.misc.SignalHandler; /** * * @author sihai */public class SignalHandlerHelper implements SignalHandler { final static String NAME_TERM = "TERM"; private final Runnable handler; public static void registerHandler(String name, Runnable handler) { SignalHandlerHelper kh = new SignalHandlerHelper(name, handler); kh.register(); } private final String name; void register() { Signal s = new Signal(name); Signal.handle(s, this); } public SignalHandlerHelper(String name, Runnable handler) { this.name = name; this.handler = handler; } @Override public void handle(Signal signal) { if (Objects.equals(this.name, signal.getName())) { handler.run(); } } } 参考: https://stackoverflow.com/questions/2541597/how-to-gracefully-handle-the-sigkill-signal-in-java