class CloseHook implements Runnable{ @Override public void run() { System.out.println("hook application is execute."); } } public class TestHook { public static void main(String[] args) throws InterruptedException { Thread t = new Thread(new CloseHook()); Runtime.getRuntime().addShutdownHook(t); System.out.println("sleeping..."); Thread.sleep(3000); } }
貌似我理解错了钩子的正确含义。如果正常运行时候当然会在结束时候自动调用关闭钩子,可是我如果强行ctrl+c什么的 这个时候关闭钩子线程还是不起作用。。。。