如何安排任务定期运行? [英] How do I schedule a task to run at periodic intervals?

查看:27
本文介绍了如何安排任务定期运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试一些代码来实现计划任务并想出了这些代码.

I was trying some codes to implement a scheduled task and came up with these codes .

import java.util.*;

class Task extends TimerTask {


    int count = 1;

    // run is a abstract method that defines task performed at scheduled time.
    public void run() {
        System.out.println(count+" : Mahendra Singh");
        count++;
    }
}

class TaskScheduling {

   public static void main(String[] args) {
       Timer timer = new Timer();


       // Schedule to run after every 3 second(3000 millisecond)
       timer.schedule( new Task(), 3000);   
   }
}

我的输出:

1  :  Mahendra Singh

我希望编译器以 3 秒的周期间隔打印一系列 Mahendra Singh,但尽管等待了大约 15 分钟,但我只得到一个输出......我该如何解决这个问题?

I expected the compiler to print a series of Mahendra Singh at periodic interval of 3 s but despite waiting for around 15 minutes, I get only one output...How do I solve this out?

推荐答案

使用 timer.scheduleAtFixedRate

public void scheduleAtFixedRate(TimerTask task,
                                long delay,
                                long period)

为重复的固定速率执行安排指定的任务,在指定的延迟后开始.随后的处决以大约有规律的时间间隔进行,以指定的时间间隔隔开.
在固定速率执行中,每次执行都相对于初始执行的调度执行时间进行调度.如果由于任何原因(例如垃圾收集或其他后台活动)导致执行延迟,两个或多个执行将快速连续发生以赶上".从长远来看,执行频率将恰好是指定周期的倒数(假设 Object.wait(long) 底层的系统时钟是准确的).

Schedules the specified task for repeated fixed-rate execution, beginning after the specified delay. Subsequent executions take place at approximately regular intervals, separated by the specified period.
In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).

固定速率执行适用于对绝对时间敏感的重复性活动,例如每小时响铃一次,或每天在特定时间运行预定维护.它也适用于执行固定执行次数的总时间很重要的重复性活动,例如每秒钟计时一次、持续十秒的倒数计时器.最后,固定速率执行适用于调度多个必须彼此保持同步的重复定时器任务.

Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.

参数:

  • task - 要安排的任务.
  • delay - 执行任务前的延迟(以毫秒为单位).
  • period - 连续任务执行之间的时间(以毫秒为单位).

抛出:

  • IllegalArgumentException - 如果延迟为负,或延迟 + System.currentTimeMillis() 为负.
  • IllegalStateException - 如果任务已被安排或取消、计时器已取消或计时器线程终止.

这篇关于如何安排任务定期运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆