2011-02-16 18 views

回答

0

我不認爲你可以改變一個正在運行的任務的間隔時間,但我想出了這個解決這個問題。這實際上是一個Java控制檯程序(因爲我只有Linux和不能在家中使用黑莓環境),我用私人靜態類只是爲了讓一個單一的文件中工作,但我認爲這個想法應該是相當清楚的:

import java.util.Scanner; 
import java.util.Timer; 
import java.util.TimerTask; 


public class Main 
{ 
    public static void main(String[] args) 
    { 
     //Start a new task which runs every 1000ms 
     TimerTest test = new TimerTest(1000L); 
     TimerTaskStarter.startTask(test); 

     //Wait for enter 
     Scanner sc = new Scanner(System.in); 
     while(!sc.nextLine().equals("")); 

     //Change the interval (actually starts a new TimerTest after the current one runs next time) 
     //since there's a new instance involved, the return value needs to be stored so it can be cancelled/controlled 
     test = test.changeIntervalAtNextRun(500); 

     //Wait for another enter   
     while(!sc.nextLine().equals("")); 
     test.cancel(); 
     System.exit(0); 
    } 

    private static class TimerTaskStarter 
    { 
     public static void startTask(TimerTest task) 
     { 
      //Basic Timer-stuff 
      Timer timer = new Timer(); 
      timer.schedule(task, task.getInterval(), task.getInterval()); 
     } 
    } 

    private static class TimerTest extends TimerTask 
    { 
     /** 
     * Current interval 
     */ 
     private long interval; 

     /** 
     * Flag to indicate interval change at next run 
     */ 
     private boolean changeInterval; 

     /** 
     * The new instance running with the new interval once started 
     */ 
     private TimerTest nextInstance; 


     public TimerTest(long interval) 
     { 
      this.interval = interval; 
      changeInterval = false; 
     } 

     @Override 
     public void run() 
     { 
      //The actual thing this task does goes here 
      System.out.println("Running task"); 

      if(changeInterval == false) 
      { 
       //Not changing interval, just keep running 
       System.out.println("Current interval is " + interval); 
      } 
      else 
      { 
       //Changing interval, cancel self and start new task 
       System.out.println("Startingting new instance with interval " + interval); 
       cancel(); 

       //Start a new task with new interval 
       TimerTaskStarter.startTask(nextInstance); 
      } 
     } 

     /** 
     * Creates a new task with given interval. This task is cancelled and new task is automatically started 
     * the next time this task runs 
     * @param newInterval Interval to run the new instance at 
     * @return new TimerTest-instance 
     */ 
     public TimerTest changeIntervalAtNextRun(long newInterval) 
     { 
      interval = newInterval; 
      changeInterval = true; 
      nextInstance = new TimerTest(interval); 
      return nextInstance; 
     } 

     /** 
     * Returns current interval 
     * @return Interval as milliseconds 
     */ 
     public long getInterval() 
     { 
      return interval; 
     } 
    } 
} 

而延伸的TimerTaskTimerTest)類是能夠創造自己的另一個實例,這是在要求的時間間隔任務本身下次運行時開始的。 changeIntervalAtNextRun返回將自動啓動當前任務下次運行時的新實例。目前的任務也在這一點上取消。

我做TimerTaskStarter.startTask靜態只是爲了讓事情變得簡單,它很可能是某種經理級持有所有當前正在運行的任務,它可以傳遞到TimerTestchangeIntervalAtNextRun和TimerTest實例將給予新的任務直接到它。多線程你可能需要某種形式的同步也發生,但我還沒有把它考慮在這個例子。希望這有助於(儘管問題是幾個月前)。