正如其他人所說,事實上,你必須進行民意調查可能表明你的系統設計有更深層次的問題......但有時候這是它的方式,所以......
如果你想處理「殺」的過程中多了幾分優雅,你可以安裝一個關閉掛鉤,當你打按Ctrl +ç其中被稱爲:
volatile boolean stop = false;
Runtime.getRuntime().addShutdownHook(new Thread("shutdown thread") {
public void run() {
stop = true;
}
});
然後定期檢查停止變量。
一個更優雅的解決方案是等待一個事件:
boolean stop = false;
final Object event = new Object();
Runtime.getRuntime().addShutdownHook(new Thread("shutdown thread") {
public void run() {
synchronized(event) {
stop = true;
event.notifyAll();
}
}
});
// ... and in your polling loop ...
synchronized(event) {
while(!stop) {
// ... do JDBC access ...
try {
// Wait 30 seconds, but break out as soon as the event is fired.
event.wait(30000);
}
catch(InterruptedException e) {
// Log a message and exit. Never ignore interrupted exception.
break;
}
}
}
或者類似的東西。
+1全面解答! – 2012-06-06 18:53:45