1

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

schedule(TimerTask task, long delay, long period) 

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

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

4

1 回答 1

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

因此,扩展类TimerTask( TimerTest) 能够创建其自身的另一个实例,该实例在下一次运行任务本身时以请求的间隔启动。changeIntervalAtNextRun返回当前任务下一次运行时将自动启动的新实例。当前任务也会在此时自行取消。

我做了TimerTaskStarter.startTask静态只是为了简单起见,它很可能是某种管理器类,包含所有当前正在运行的任务,可以传递到TimerTest'schangeIntervalAtNextRun中,而 TimerTest 实例会将新任务直接提供给它。对于多个线程,您可能还需要进行某种同步,但在此示例中我没有考虑到它。希望这会有所帮助(尽管这个问题已经存在几个月了)。

于 2011-05-15T10:14:24.707 回答