【问题标题】:Is there a Java equivalent of the Python sched module?是否有 Python sched 模块的 Java 等价物?
【发布时间】:2012-01-26 10:31:10
【问题描述】:

Raymond Hettinger 发布了snippet,他使用标准 Python 库中的 sched 模块以特定速率(每秒 N 次)调用函数。我想知道Java中是否有等效的库。

【问题讨论】:

    标签: java python function scheduling


    【解决方案1】:

    看看http://quartz-scheduler.org/

    Quartz 是一种功能齐全的开源作业调度服务,可以与几乎任何 Java EE 或 Java SE 应用程序集成或一起使用 - 从最小的独立应用程序到最大的电子商务系统。

    【讨论】:

      【解决方案2】:

      看看 java.util.Timer。

      你可以找到使用here的例子

      你也可以考虑Quartz,功能更强大,可以组合使用 带弹簧 这是example

      这是我使用 java.util.Timer 你提到的代码 sn-p 的等价物

      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.");
            }
      }
      

      【讨论】:

      • Timer 类和 Quartz 库都提供了类似 cron 的接口,将作业安排在特定时间或每 N 个时间单位发生一次。我需要的是一个调度器来安排任务以达到特定的速率,例如每个时间单位调用 N 个函数(方法)。就像我在 sn-p 中发布的@Tichodroma
      • @dkart,正如您在我的回答中看到的那样,创建具有您期望的行为的应用程序非常容易......除非我错过了什么
      • 非常感谢@C.Champagne 我认为这就是我需要的!
      • 不客气@dkart!请不要忘记标记您对问题的回答。
      【解决方案3】:

      轻量级选项是ScheduledExecutorService

      与 python sn-p 大致等效的 Java 代码是:

      private final ScheduledExecutorService scheduler = 
             Executors.newScheduledThreadPool(1);
      
      public ScheduledFuture<?> newTimedCall(int callsPerSecond, 
          Callback<T> callback, T argument) {
          int period = (1000 / callsPerSecond);
          return 
              scheduler.scheduleAtFixedRate(new Runnable() {
                  public void run() {
                      callback.on(argument);
                  }
              }, 0, period, TimeUnit.MILLISECONDS);
      }
      

      留给读者的练习:

      • 定义回调接口
      • 决定如何处理返回的未来
      • 记得关闭执行器

      【讨论】:

      • +1 可能是最好的解决方案。有趣的是,Java sn-p 并没有比 Python 等价物长多少。
      • 谢谢!我确实巧妙地遗漏了一些样板:)
      【解决方案4】:

      java.util.Timer 怎么样?见this related answer

      【讨论】:

        【解决方案5】:

        使用java.util.Timer class怎么样?

        你可以找到示例代码here

        【讨论】:

          猜你喜欢
          • 2019-06-11
          • 2012-05-12
          • 2011-05-28
          • 2016-07-05
          • 2020-09-20
          • 2021-03-29
          • 2012-07-17
          • 2012-06-14
          • 1970-01-01
          相关资源
          最近更新 更多