-1
我會鏈接從我的線程中每5秒執行一次方法。我班的輪廓如下。從Java線程中運行計時器
private static Timer timer = new Timer();
public class myThread implements Runnable {
public void run() {
//listens for incoming messages
while(true) {
//process queue
timer.schedule(new TimerTask() {
process_queue();
}, 5*1000);
}
}
public static void process_queue() {
//processes queue
System.out.println("boom");
}
}
任何幫助,將不勝感激。
你面臨什麼問題? –
我需要能夠不斷偵聽傳入消息,但每隔5秒從數據庫處理隊列。我在while循環中運行定時器時遇到問題。 – hawx
重複安排一次任務。您調用'schedule'的線程無關緊要,'Timer'爲計劃任務維護自己的線程。 –