2012-08-02 208 views
0

我想在一個類中使用一個函數來啓動和停止使用布爾值鏈接到int值的計時器。因此,例如,如果我啓動了一個int爲0的計時器,那麼這將是timer0,如果它是3,那麼timer3等等。在Java中停止計時器事件

我遇到的問題是計時器似乎開始確定,但是當我向他們發送一個假布爾來阻止他們時,他們將繼續運行,因此我需要知道如何正確阻止他們。

在Class.java代碼:

public void Event(final int value, boolean run, int time){ 

    if(run){ 
     System.out.println(run); 

     Timer timer = new Timer(); 

     timer.schedule(new TimerTask() { 
      public void run() { 
       // The needed code will go here 
       System.out.println(value + " Event run"); 
      } 
     }, 0, time); // Every second 
    } else { 
    } 

} 

然後我Main.java代碼:

System.out.println("Start Timer 0 Event"); 
r.Event(0, true, 1000); 

System.out.println("Start Timer 1 Event"); 
r.Event(1, true, 250); 

System.out.println("Start Timer 2 Event"); 
r.Event(2, true, 250); 

r.Event(0, false, 1000); // Not Working as i need 
System.out.println("Stop Timer 0 Event"); 

基本上我只是想有套活動得到重複一次集直到我阻止他們的時間量,並且可能有許多人在一起運行。如果定時器不是這樣做的最好方法,那麼替代方案會很好,但它需要按照描述的相同方式工作。


根據要求,這裏是我的計時器的可運行代碼。

MyClass.java:

package com.z; 

import java.awt.*; 
import java.util.*; 
import java.util.TimerTask; 

public class MyClass { 

//////////////////////////////////////////////////////////// 
//Name: Event (BROKEN) 
//////////////////////////////////////////////////////////// 
public void Event(final int value, boolean run, int time){ 

    Timer timer = new Timer("" + value, true); 

    if(run){ 
     System.out.println(run); 

     timer.schedule(new TimerTask() { 
      public void run() { 
       // Code here 
       System.out.println(value + " Event run"); 
      } 
     }, 0, time); // Every second 
    } 

    if (!run) { 
     timer.cancel(); 
    } 
} 

} 

Example.java:

package com.z; 

import java.awt.*; 
import java.awt.event.*; 

public class Example { 

    public static void main(String[] args) { 

    MyClass r = new MyClass(); 

    //////////////////////////////////////////////////////////// 
    // Event (BROKEN) 
    //////////////////////////////////////////////////////////// 
    System.out.println("Start Timer 0 Event"); 
    r.Event(0, true, 1000); 

    System.out.println("Start Timer 1 Event"); 
    r.Event(1, true, 250); 

    r.Event(0, false, 1000); 
    System.out.println("Stop Timer 0 Event"); 

    } 
} 

回答

1

timer.schedule方法是採取3個ARGS重複執行,因此,如果運行是真實的,計時器將開始執行任務。

如果您想停止計時器,您可以始終呼叫timer.cancel,但您需要保存對Event方法之外的計時器的引用。

Timer的javadoc應該在這裏幫助http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

編輯:這裏是如何這可能工作

Timer startTimer(final int value, final long time) { 
    Timer timer = new Timer("Timer" + value); 
    timer.schedule(new TimerTask() { 
      public void run() { 
       // Code here 
       System.out.println(value + " Event run"); 
      } 
     }, 0, time); // Every second 
    return timer; 
} 

Timer t0 = startTimer(0,1000); 
Timer t1 = startTimer(1,1000); 

// stop t0 
t0.cancel(); 
+1

你可以發佈一個簡短的,完整的可運行的代碼不適合你的例子嗎? – 2012-08-02 12:12:34

+1

這不是可運行的代碼(或者至少不容易運行)。你可以添加可以複製/粘貼並輕鬆運行的主要方法嗎?請使用代碼編輯您的帖子或添加到pastebin。 – 2012-08-02 12:37:36

+1

一旦你在Event方法中啓動定時器,你就無法取消同一個定時器,因爲它是一個局部變量。爲什麼不直接從Event方法返回計時器(可能需要重命名該方法),而不是運行參數。然後,當你準備好停止那個計時器時,只需對它停止。 – 2012-08-02 13:24:45

0

定時器的例子將運行5秒後預定的,並保持運行的每個1秒直到timer.cancel被調用。

Timer timer = new Timer(); 

    timer.schedule(new TimerTask(){ 

     @Override 
     public void run() { 
      System.out.println("Timer task"); 

     } 

    }, 5000,1000); 
+0

謝謝,但我已經有一個工作計時器,問題是讓它停止時,從我的班級使用一個單一的功能。我嘗試使用:\t if(!run){ \t \t timer.cancel(); \t}但它繼續運行。 – zeddex 2012-08-02 12:11:55

1

另一種解決方案是不使用Timer,因爲每個定時器都是單線程,並且其運行耗費資源。您可以創建一個檢查任務隊列的線程。任務可以使用週期參數進行調整。調度意味着將任務添加到隊列中。並且主線程檢查每個Task是否等於隊列中足夠的時間長度,以及它的週期值。

while (running) { 
    Timer timer = timerQueue.poll(); 
    if (timer.nextExecutionTime < System.currentTimeMillis()) { 
     timer.timerExpire(); 
    } 
    else { 
     // nextexecution time degismeden yeniden schedule edilir. 
     reschedule(timer); 
    } 
    }// while 

在這個例子中,Timer是我自己的持有TimerListener的類。在啓動Timer任務操作的同時,在timerExpire方法中寫入。

+0

謝謝,我試過你的代碼,並用運行替換運行使用我的函數輸入布爾,但是Eclipse突出顯示timerQueue,nextExecutionTime,timerExpire和reschedule作爲錯誤。 – zeddex 2012-08-02 12:09:20