12

如何在运行时更改 Timer 的周期?

    Timer timer = new Timer();

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

             // read new period
             period = getPeriod();

             doSomething();

        }
    }, 0, period);
4

4 回答 4

8

您不能直接执行此操作,但您可以取消任务Timer并在所需时间段内重新安排它们。

没有getPeriod方法。

于 2011-06-29T12:03:28.173 回答
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);
}
于 2015-11-16T09:41:58.080 回答
0

您可以使用以下类在运行时更改 a 的执行周期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();
    }

}

Timer 可以这样启动:

EditablePeriodTimerTask editableTimerTask =
    new EditablePeriodTimerTask(runnable, () -> getPeriod());
editableTimerTask.updateTimer();

runnable您要执行的真正任务在哪里,并getPeriod()提供任务执行之间的时间段。当然可以根据您的要求进行更改。

于 2018-08-10T05:53:09.357 回答
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

}
于 2021-08-13T03:31:36.980 回答