2012-11-23 35 views
0

我有java控制檯應用程序什麼將作爲守護進程運行,我希望使用Timer.schedule每n秒執行一次任務。防止退出計劃任務

Timer timer = new Timer(true); 
timer.schedule(new ATask(), 0,10000); 
//insert block here  

什麼是阻止退出的最佳方式?

回答

1

嘗試Thread#join()您當前的主題。

Timer timer = new Timer(true); 
timer.schedule(new ATask(), 0,10000); 
Thread currentThread = Thread.currentThread(); 
currentThread.join(); 
+0

我不想終止,我需要一個塊,否則程序在timer.schedule()之後立即退出...... – yuris

+0

它會工作。我測試過了。 –

+0

仍然有問題,如果我打電話timer.cancel(),定時器停止但主線程卡住 – yuris