如何在运行时更改我的TimerTask的执行周期?

14

我如何在运行时更改计时器的周期?

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {

             // read new period
             period = getPeriod();

             doSomething();

        }
    }, 0, period);

不幸的是,我认为您将不得不使用新周期安排一个新的计时器。无论如何,您应该使用ScheduledExecutorService - mre
scheduleAtFixedRate(TimerTask task, Date firstTime, long period); 可以使用此方法。 - peshkira
@peshkira,OP已经知道那个方法了,但他正在问是否可以在运行时更改周期,这是不可能的。 - mre
4个回答

8

直接这样做是不行的,但您可以取消 Timer 上的任务,并重新安排具有所需周期的任务。

没有 getPeriod 方法。


7
你不能重新安排已存在的TimerTask。一旦被取消,它就再也不会运行了。你需要创建一个新的TimerTask实例。 - Spycho

4
您可以这样做:
private int period= 1000; // ms

private void startTimer() {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        public void run() {
            // do something...
            System.out.println("period = " + period);
            period = 500;   // change the period time
            timer.cancel(); // cancel time
            startTimer();   // start the time again with a new period time
        }
    }, 0, period);
}

0
你可以使用以下类在运行时更改TimerTask的执行周期。
正如已经解释的那样,它实际上不能更改周期,而必须取消并重新安排任务:
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.function.Supplier;

/**
 * {@link TimerTask} with modifiable execution period.
 * 
 * @author Datz
 */
public class EditablePeriodTimerTask extends TimerTask {

    private Runnable task;
    private Supplier<Long> period;
    private Long oldP;

    /**
     * Constructor with task and supplier for period
     * 
     * @param task the task to execute in {@link TimerTask#run()}
     * @param period a provider for the period between task executions
     */
    public EditablePeriodTimerTask(Runnable task, Supplier<Long> period) {
        super();
        Objects.requireNonNull(task);
        Objects.requireNonNull(period);
        this.task = task;
        this.period = period;
    }

    private EditablePeriodTimerTask(Runnable task, Supplier<Long> period, Long oldP) {
        this(task, period);
        this.oldP = oldP;
    }

    public final void updateTimer() {
        Long p = period.get();
        Objects.requireNonNull(p);
        if (oldP == null || !oldP.equals(p)) {
            System.out.println(String.format("Period set to: %d s", p / 1000));
            cancel();
            new Timer().schedule(new EditablePeriodTimerTask(task, period, p), p, p);
            // new Timer().scheduleAtFixedRate(new EditablePeriodTimerTask(task, period), p, p);
        }
    }

    @Override
    public void run() {
        task.run();
        updateTimer();
    }

}

计时器可以这样启动:
EditablePeriodTimerTask editableTimerTask =
    new EditablePeriodTimerTask(runnable, () -> getPeriod());
editableTimerTask.updateTimer();

runnable 是你要执行的真正任务,getPeriod() 提供了任务执行之间的时间间隔。当然,这取决于你的需求。


0
Timer timer = new Timer();  //should not create timer again 
private long periord = 1000; // periord is changed at runtime

public void runTaskPeriord() {
    
    TimerTask task = new TimerTask() {      
        @Override
        public void run() {
            log.debug("Task run" );
            if(periord <= 3000) {
                this.cancel();  // cancel this task to run new task
                periord += 1000; 
                runTaskPeriord();
            }
        }
    };
    
    timer.schedule(task, periord, periord);
    int countDeletedTasks = timer.purge(); // remove cancel task from timer

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接