Java Timer scheduleAtFixedRate()方法与示例
计时器类scheduleAtFixedRate()
方法
语法:
public void scheduleAtFixedRate (TimerTask tt, Date ft, long period); public void scheduleAtFixedRate (TimerTask tt, long de, long period);
scheduleAtFixedRate()方法在java.util包中可用。
scheduleAtFixedRate(TimerTasktt,Dateft,longperiod)方法用于调度给定任务,以在给定时间开始重复执行恒定速率。
scheduleAtFixedRate(TimerTasktt,longdelay,longperiod)方法用于调度给定任务,以便在给定延迟后开始重复执行恒定速率的任务。
这些方法在调度任务时可能会引发异常。
IllegalArgumentException:当任何一个参数不在范围内时,可能引发此异常。
IllegalStateException:在计划或取消任务时,可能会引发此异常。
这些是非静态方法,只能通过类对象访问,如果尝试使用类名称访问这些方法,则会收到错误消息。
参数:
在第一种情况下,为scheduleAtFixedRate(TimerTasktt,Dateft,longperiod)
TimerTasktt–表示要安排的计时器任务。
日期ft–表示要安排的计时器任务。
长周期–表示两次任务执行之间的时间(以毫秒为单位)。
在第一种情况下,为scheduleAtFixedRate(TimerTasktt,longde,longperiod)
TimerTasktt–表示要安排的计时器任务。
longde–表示第一次执行任务。
长周期–表示两次任务执行之间的时间(以毫秒为单位)。
返回值:
在这两种情况下,该方法的返回类型都是空的,它什么也不返回。
范例1:
// Java程序演示示例 的//scheduleAtFixedRate()的方法 //计时器 import java.util.*; public class ScheduleAtFixedRateOfTimer { public static void main(String[] args) { //实例化一个TimerTask并 //计时器 object TimerTask task = new ScheduleTask(); Timer tmr = new Timer(); System.out.println("tmr.scheduleAtFixedRate(task, new Date(), 1000): "); //通过使用scheduleAtFixedRate(task,date,period)方法isto- //以固定的速率安排任务 //重复方式,并在给定的时间开始1000毫秒 tmr.scheduleAtFixedRate(task, new Date(), 1000); } } class ScheduleTask extends TimerTask { //在此方法中定义的任务 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) { //实例化一个TimerTask并 //计时器 object TimerTask task = new ScheduleTask(); Timer tmr = new Timer(); System.out.println("tmr.scheduleAtFixedRate(task, 50, 330): "); //通过使用scheduleAtFixedRate(task,delay,period)方法isto- 以固定的速率安排任务 //重复方式,并在给定延迟后开始 tmr.scheduleAtFixedRate(task, 100, 800); } } class ScheduleTask extends TimerTask { //在此方法中定义的任务 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