Is there a Java equivalent of the Java sched module?

Raymond Hettinger released a snippet that uses the sched module provided in the standard Python library to call functions at a specific rate (n times per second) I wonder if there is an equivalent Library in Java

Solution

Look at Java util. Timer.

You can find examples of using here

You can also consider quartz, which is more powerful and can be used in combination with spring. This is an example

This is the Java. Java that I use the code snippet you mentioned util. Equivalent of 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.");
      }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>