timer java
计时器类scheduleAtFixedRate()方法 (Timer Class scheduleAtFixedRate() method)
Syntax:
句法:
public void scheduleAtFixedRate (TimerTask tt, Date ft, long period);
public void scheduleAtFixedRate (TimerTask tt, long de, long period);
scheduleAtFixedRate() method is available in java.util package.
scheduleAtFixedRate()方法在java.util包中可用。
scheduleAtFixedRate (TimerTask tt, Date ft, long period) method is used to schedule the given task for constant rate execution repeatedly starting at the given time.
scheduleAtFixedRate(TimerTask tt,Date ft,long period)方法用于调度给定任务,以便在给定时间开始以恒定速率重复执行。
scheduleAtFixedRate (TimerTask tt, long delay, long period) method is used to schedule the given task for constant rate execution repeatedly starting after the given delay.
scheduleAtFixedRate(TimerTask tt,long delay,long period)方法用于调度给定任务,以便在给定延迟后开始重复执行恒定速率的任务。
These methods may throw an exception at the time of the scheduling task.
这些方法在调度任务时可能会引发异常。
- IllegalArgumentException: This exception may throw when any one of the parameters is not in a range.IllegalArgumentException :当任何一个参数不在范围内时,可能引发此异常。
- IllegalStateException: This exception may throw when the task was scheduled or canceled already.IllegalStateException :在计划或取消任务时,可能会引发此异常。
These are non-static methods and it is accessible with the class object only and if we try to access these methods with the class name then we will get an error.
这些是非静态方法,只能通过类对象访问,如果尝试使用类名称访问这些方法,则会收到错误消息。
Parameter(s):
参数:
In the first case, scheduleAtFixedRate (TimerTask tt, Date ft, long period)
在第一种情况下,为scheduleAtFixedRate(TimerTask tt,Date ft,long period)
- TimerTask tt – represents the timer task to be scheduled.
- TimerTask tt –表示要安排的计时器任务。
- Date ft – represents the timer task to be scheduled.
- 日期ft –表示要安排的计时器任务。
- long period – represents the time in milliseconds between task executions.
- 长周期 –表示两次任务执行之间的时间(以毫秒为单位)。
In the first case, scheduleAtFixedRate (TimerTask tt, long de, long period)
在第一种情况下,为scheduleAtFixedRate(TimerTask tt,long de,long period)
- TimerTask tt – represents the timer task to be scheduled.
- TimerTask tt –表示要安排的计时器任务。
- long de – represents the first time at which the task is implemented.
- long de –表示第一次执行任务。
- long period – represents the time in milliseconds between task executions.
- 长周期 –表示两次任务执行之间的时间(以毫秒为单位)。
Return value:
返回值:
In both the cases, the return type of the method is void, it returns nothing.
在这两种情况下,方法的返回类型都是void ,它什么也不返回。
Example 1:
范例1:
// Java program to demonstrate the example
// of scheduleAtFixedRate() method of
// Timer
import java.util.*;
public class ScheduleAtFixedRateOfTimer {
public static void main(String[] args) {
// Instantaites a TimerTask and
// Timer object
TimerTask task = new ScheduleTask();
Timer tmr = new Timer();
System.out.println("tmr.scheduleAtFixedRate(task, new Date(), 1000): ");
// By using scheduleAtFixedRate(task,date,period) method isto
// schedule the task at a constant rate in a
// repeated manner and starts at the given time 1000 ms
tmr.scheduleAtFixedRate(task, new Date(), 1000);
}
}
class ScheduleTask extends TimerTask {
// Task defined in this method
public void run() {
System.out.println("Out Of Stock...Keep Working");
}
}
Output
输出量
tmr.scheduleAtFixedRate(task, new Date(), 1000):
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Out Of Stock...Keep Working
Example 2:
范例2:
import java.util.*;
public class ScheduleAtFixedRateOfTimer {
public static void main(String[] args) {
// Instantaites a TimerTask and
// Timer object
TimerTask task = new ScheduleTask();
Timer tmr = new Timer();
System.out.println("tmr.scheduleAtFixedRate(task, 50, 330): ");
// By using scheduleAtFixedRate(task,delay,period) method isto
// schedule the task at a constant rate in a
// repeated manner and starts after the given delay
tmr.scheduleAtFixedRate(task, 100, 800);
}
}
class ScheduleTask extends TimerTask {
// Task defined in this method
public void run() {
System.out.println("Out of Stock...Keep Working");
}
}
Output
输出量
tmr.scheduleAtFixedRate(task, 50, 330):
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
Out of Stock...Keep Working
翻译自: https://www.includehelp.com/java/timer-scheduleatfixedrate-method-with-example.aspx
timer java