Java优雅停止服务 ShutdownHook使用 Java开发使用ShutdownHook优雅的停止服务 Java开发使用ShutdownHook优雅的停止服务 import java.io.IOException; public class Main { public static void main(String[] args) { System.out.println("我是主线程"); Runtime.getRuntime().addShutdownHook(new Thread(() -> { System.out.println("程序结束之前最后的反抗"); })); new Thread(() -> { System.out.println("==============IDE debug=================="); System.out.println("press ENTER to call System.exit(0) "); System.out.println("==============IDE debug=================="); try { System.out.println(System.in.read()); } catch (IOException e) { e.printStackTrace(); } //System.exit(0); System.exit(1); }).start(); } }