开发者

timertask in blackberry, change schedule time without interrupting the execution?

I'm using TimerTask to run some back开发者_如何学Goground process..

schedule(TimerTask task, long delay, long period) 

i want to change the long period value without interrupting the task, from the next cycle of execution it should use the modified time..

is that possible without interrupting or should i cancel timerTask and restart again?


I don't think you can change the interval of a running task, but I came up with this to tackle the problem. This is actually a Java-console program (as I have only Linux, and can't use the BlackBerry-environment at home), and I used private static classes just to make it work within a single file, but I think the idea should be quite clear:

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;
        }
    }
}

So the class extending TimerTask (TimerTest) is capable of creating another instance of itself, which is started at requested interval the next time the task itself is run. changeIntervalAtNextRun returns the new instance which will be started automatically when the current task next runs. The current task also cancels itself at that point.

I made TimerTaskStarter.startTask static just to keep things simple, it could very well be some sort of manager-class holding all the currently running tasks, which could be passed into TimerTest's changeIntervalAtNextRun, and the TimerTest-instance would give the new task directly to it. With multiple threads you'd probably need some sort of synchronization to occur also, but I haven't taken it into account in this example. Hope this helps (although the question is a couple of months old).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜