Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading blackberry中的timertask,在不中断执行的情况下更改计划时间?_Multithreading_Blackberry_Timer_Timertask - Fatal编程技术网

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

Multithreading blackberry中的timertask,在不中断执行的情况下更改计划时间?,multithreading,blackberry,timer,timertask,Multithreading,Blackberry,Timer,Timertask,我正在使用来运行一些后台进程 schedule(TimerTask task, long delay, long period) 我希望在不中断任务的情况下更改长周期值,从下一个执行周期开始,它应该使用修改后的时间 这是否可能不中断,或者我应该取消timerTask并重新启动?我认为您无法更改正在运行的任务的间隔,但我提出了这个方法来解决这个问题。这实际上是一个Java控制台程序(因为我只有Linux,在家里不能使用BlackBerry环境),我使用私有静态类只是为了让它在一个文件中工作,但

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

schedule(TimerTask task, long delay, long period) 
我希望在不中断任务的情况下更改长周期值,从下一个执行周期开始,它应该使用修改后的时间


这是否可能不中断,或者我应该取消timerTask并重新启动?

我认为您无法更改正在运行的任务的间隔,但我提出了这个方法来解决这个问题。这实际上是一个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
changeIntervalAtNextRun
,TimerTest实例将直接将新任务交给它。对于多线程,您可能还需要某种同步,但在本例中我没有考虑到这一点。希望这能有所帮助(尽管这个问题已经提出几个月了)