Timer类scheduleAtFixedRate()方法
用法:
public void scheduleAtFixedRate (TimerTask tt, Date ft, long period); public void scheduleAtFixedRate (TimerTask tt, long de, long period);
- scheduleAtFixedRate() 方法可在
java.util
包。 - scheduleAtFixedRate (TimerTask tt, Date ft, long period) 方法用于调度给定任务以在给定时间重复开始以恒定速率执行。
- scheduleAtFixedRate (TimerTask tt, long delay, long period) 方法用于调度给定任务以在给定延迟后重复开始以恒定速率执行。
- 这些方法可能会在调度任务时抛出异常。
- IllegalArgumentException:当任何一个参数不在范围内时,可能会抛出此异常。
- IllegalStateException:当任务已被安排或取消时,可能会抛出此异常。
- 这些是非静态方法,只能通过类对象访问,如果我们尝试使用类名访问这些方法,则会出现错误。
参数:
- 在第一种情况下,scheduleAtFixedRate (TimerTask tt, Date ft, long period)
TimerTask tt
- 表示要调度的计时器任务。Date ft
- 表示要调度的计时器任务。long period
- 表示任务执行之间的时间(以毫秒为单位)。
- 第一种情况,scheduleAtFixedRate(TimerTask tt, long de, long period)
TimerTask tt
- 表示要调度的计时器任务。long de
- 代表第一次执行任务。long period
- 表示任务执行之间的时间(以毫秒为单位)。
返回值:
在这两种情况下,方法的返回类型都是void
,它什么都不返回。
范例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");
}
}
输出
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
范例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");
}
}
输出
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
相关用法
- Java Timer schedule()用法及代码示例
- Java Timer purge()用法及代码示例
- Java Timer cancel()用法及代码示例
- Java TimerTask scheduledExecutionTime()用法及代码示例
- Java TimerTask run()用法及代码示例
- Java TimerTask cancel()用法及代码示例
- Java TimeZone setDefault()用法及代码示例
- Java TimeUnit values()用法及代码示例
- Java TimeZone getDisplayName(boolean, int)用法及代码示例
- Java TimeUnit toSeconds()用法及代码示例
- Java TimeZone observesDaylightTime()用法及代码示例
- Java TimeZone getDisplayName(Locale locale)用法及代码示例
- Java TimeZone getDefault()用法及代码示例
- Java TimeZone useDaylightTime()用法及代码示例
- Java SQL Timestamp before()用法及代码示例
- Java TimeUnit toNanos()用法及代码示例
- Java TimeZone hasSameRules()用法及代码示例
- Java TimeUnit toDays()用法及代码示例
- Java SQL Timestamp getNanos()用法及代码示例
- Java TimeZone setRawOffset()用法及代码示例
注:本文由纯净天空筛选整理自Preeti Jain大神的英文原创作品 Java Timer scheduleAtFixedRate() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。