Question: Raymond Hettinger has posted a [snippet](http://code.activestate.com/recipes/413137-call-a-functionmethod-x-times-per-second/#c3) 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.

Answer: 
A light weight option is the [ScheduledExecutorService](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html).

The roughly equivalent Java code to the python snippet would be:

```
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);
}

```

Exercises left to the reader:

* Define the Callback interface
* Decide what to do with the returned future
* Remember to shut the executor down
How about using the [java.util.Timer class](http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html)?

You can find the example code [here](http://www.roseindia.net/java/example/java/util/CertainAndRepeatTime.shtml)