加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

是否有Java sched模块的Java等价物?

发布时间:2020-12-15 04:56:35 所属栏目:Java 来源:网络整理
导读:Raymond Hettinger发布了一个 snippet,他使用标准Python库中提供的sched模块,以便以特定的速率调用函数(每秒N次).我想知道Java中是否有一个等价的库. 解决方法 看看java.util.Timer. 您可以找到使用here的示例 您还可以考虑Quartz,它功能更强大,可以组合使用
Raymond Hettinger发布了一个 snippet,他使用标准Python库中提供的sched模块,以便以特定的速率调用函数(每秒N次).我想知道Java中是否有一个等价的库.

解决方法

看看java.util.Timer.

您可以找到使用here的示例

您还可以考虑Quartz,它功能更强大,可以组合使用
与春天
这是example

这是我使用您提到的代码片段的java.util.Timer的等价物

package perso.tests.timer;

import java.util.Timer;
import java.util.TimerTask;

public class TimerExample  extends TimerTask{

      Timer timer;
      int executionsPerSecond;

      public TimerExample(int executionsPerSecond){
          this.executionsPerSecond = executionsPerSecond;
        timer = new Timer();
        long period = 1000/executionsPerSecond;
        timer.schedule(this,200,period);
      }

      public void functionToRepeat(){
          System.out.println(executionsPerSecond);
      }
        public void run() {
          functionToRepeat();
        }   
      public static void main(String args[]) {
        System.out.println("About to schedule task.");
        new TimerExample(3);
        new TimerExample(6);
        new TimerExample(9);
        System.out.println("Tasks scheduled.");
      }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读