在哪裏以及如何在一個類中實現addShutdownHook,它沒有主要的方法?這可以用來殺死所有由該類初始化的活動套接字嗎?addShutdownHook的實現
0
A
回答
1
這可能爲你工作,
public class AddShutdownHookSample {
public void attachShutDownHook(){
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
System.out.println("Inside Add Shutdown Hook");
}
});
System.out.println("Shut Down Hook Attached.");
}
}
而且在主要方法:
public static void main(String[] args) {
AddShutdownHookSample sample = new AddShutdownHookSample();
sample.attachShutDownHook();
System.out.println("Last instruction of Program....");
System.exit(0);
}
描述你正在嘗試做的,並顯示在您擁有的是非常精確的點整件事麻煩這會讓其他人更容易幫助你。
0
下面是一個例子,這可以幫助你
public class RuntimeDemo {
// a class that extends thread that is to be called when program is exiting
static class Message extends Thread {
public void run() {
System.out.println("Bye.");
}
}
public static void main(String[] args) {
try {
// register Message as shutdown hook
Runtime.getRuntime().addShutdownHook(new Message());
// print the state of the program
System.out.println("Program is starting...");
// cause thread to sleep for 3 seconds
System.out.println("Waiting for 3 seconds...");
Thread.sleep(3000);
// print that the program is closing
System.out.println("Program is closing...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
+0
感謝Anptk的幫助..我完成了我的工作.. – Atmaram 2015-07-10 09:14:13
0
就像這樣......
static {
Runtime.getRuntime().addShutdownHook (new Thread() {
public void run() {
server.shutdown();
}
});
}
相關問題
- 1. 爪哇addShutdownHook方法
- 2. 如何強制呼叫addShutdownHook(...)
- 3. 。bcrypt的實現,實現HashAlgorithm?
- 4. addShutdownHook和setUncaughtExceptionHandler在java中無法正常工作
- 5. PickerView在Titanium中實現的TableView實現
- 6. 在python中實現R表的實現
- 7. CPython內部實現的文檔實現
- 8. 實現polynimial類實現
- 9. NSArray的實現
- 10. malloc的實現?
- 11. getUTF8Length的實現
- 12. Dynatable的實現
- 13. 的hashCode實現
- 14. MiniMax的實現
- 15. 的FIFO實現
- 16. BufferedIterator的實現
- 17. MvxBindableCollectionViewSource的實現
- 18. simple_search_text的實現
- 19. Maven的實現
- 20. GestureDetectorCompat的實現
- 21. Qbservable的實現
- 22. 的Deque實現
- 23. crawler4j的實現
- 24. __builtin_clz的實現
- 25. 自動實現一個接口來封裝現有的實現
- 26. 實現
- 27. 實現
- 28. AVL樹的實現
- 29. 實現爲Winform的
- 30. Laravel的SweetAlert實現
你有沒有看:http://stackoverflow.com/questions/ 8722826 /當我需要調用這個方法運行時getruntime addshutdownhook 這不是真的重複,但我認爲你可以在那裏學習的東西? 另請參見:http://hellotojavaworld.blogspot.se/2010/11/runtimeaddshutdownhook.html – 2014-10-09 06:44:53
@SamuelÅslund:謝謝你的幫助..我已經搜索過它,但沒有得到這個鏈接.. – Atmaram 2014-10-10 11:37:56
@SamuelÅslund:addShutdownHook doesn當我們使用Kill -9命令在Linux中終止服務器時不起作用。任何其他解決方案相同? – Atmaram 2014-10-13 11:52:28