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

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

问题描述

我正在尝试使用某些代码来实现计划任务,并提出了这些代码。

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

I期望编译器以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.

参数:


  • 任务 - 要安排的任务。

  • 延迟 - 在执行任务之前以毫秒为单位的延迟。

  • period - 连续任务执行之间的时间(以毫秒为单位)。

抛出:


  • IllegalArgumentException - 如果延迟为负,或延迟+ System.currentTimeMillis()为负。

  • IllegalStateException - 如果任务已经安排或取消,计时器被取消或计时器线程终止。

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

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