黑莓中的timertask,在不中断执行的情况下更改计划时间?

时间:2011-02-16 08:28:52

标签: multithreading blackberry timer timertask

我正在使用TimerTask来运行一些后台进程..

schedule(TimerTask task, long delay, long period) 

我想在不中断任务的情况下更改长周期值,从下一个执行周期开始,它应该使用修改时间..

可以不中断或者我应该取消timerTask并重新启动吗?

1 个答案:

答案 0 :(得分:0)

我认为你不能改变正在运行的任务的间隔,但我想出了解决这个问题的方法。这实际上是一个Java控制台程序(因为我只有Linux,不能在家中使用BlackBerry环境),我使用私有静态类只是为了使它在一个文件中工作,但我认为这个想法应该非常清楚:

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设为静态只是为了简单起见,它很可能是某种类型的管理器类,它包含所有当前正在运行的任务,这些任务可以传递到TimerTest的{​​{1}并且TimerTest实例将直接向它提供新任务。对于多个线程,您可能还需要进行某种同步,但在此示例中我没有考虑到这一点。希望这会有所帮助(虽然这个问题已经有几个月了)。