scheduleAtFixedRate
时间: 2023-10-12 19:09:20 浏览: 92
Java定时任务schedule和scheduleAtFixedRate的异同
scheduleAtFixedRate is a method in the Java Timer class that allows you to schedule a task to be executed repeatedly at a fixed interval of time. This method takes four parameters:
1. A TimerTask object that represents the task to be executed.
2. A long value that specifies the delay before the first execution of the task.
3. A long value that specifies the interval between subsequent executions of the task.
4. A boolean value that determines whether the task should be executed even if the previous execution is still running.
The scheduleAtFixedRate method ensures that the task is executed at the specified interval even if the previous execution takes longer to complete than the specified interval. However, this can lead to overlapping executions of the task if the task takes longer to complete than the specified interval.
Here is an example of using the scheduleAtFixedRate method to schedule a task to be executed every 10 seconds:
```
Timer timer = new Timer();
TimerTask task = new TimerTask() {
public void run() {
System.out.println("Task executed at " + new Date());
}
};
timer.scheduleAtFixedRate(task, 0, 10000);
```
阅读全文