Raymond Hettinger has posted a snippet where he uses the sched module available in the standard Python library in order to call functions with a specific rate (N times per second). I wonder if there is an equivalent library in Java.
Take a look at http://quartz-scheduler.org/
Quartz is a full-featured, open source job scheduling service that can be integrated with, or used along side virtually any Java EE or Java SE application - from the smallest stand-alone application to the largest e-commerce system.
Have a look at java.util.Timer.
You can find an example of use here
You can also consider Quartz, which is much more powerful and can be used in combination with Spring Here is an example
Here is my equivalent using java.util.Timer of the code snippet you mentionned
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.");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With