timer java_Java Timer scheduleAtFixedRate()方法与示例

本文详细介绍了Java中Timer类的scheduleAtFixedRate方法,该方法用于以固定频率重复执行任务。文章提供了两个示例,展示了如何使用该方法在给定时间和延迟后开始执行任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值