我正在尝试在Spring Gradle应用程序中通过Runtime.getRuntime().addShutdownHook()
添加一个关闭钩子。
我尝试从this exact tutorial向Application.java类添加一个匿名线程子类,所以它看起来像这样:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
Thread thread = new Thread() {
public void run() {
System.out.println("Shutdown Thread!");
}
};
Runtime.getRuntime().addShutdownHook(thread);
SpringApplication.run(Application.class, args);
}
}
但是,我没有看到理想的行为:打印"关机线程!"退出申请。这里出了什么问题?
编辑:我一直在运行它的终端上使用control + c
关闭它。我在OSX终端上运行它