What’s the best way to make code run at regular intervals in Clojure ? I’m currently using java.util.concurrent.ScheduledExecutorService, but that’s Java – is there a Clojure way of scheduling code to run at regular intervals, after a delay, cancellably ? All the Clojure code examples I’ve seen use Thread/sleep, which also seems too Java.
Share
Well worth looking at the source code for Overtone, in particular the code for scheduling events at a particular time.
It’s a music synthesis system so you have to hope they have the timing code right!!
Also they have helpfully separated the timing code out into a separate project (overtone/at-at) so that you can easily import it if you want. This provides a nice Clojure wrapper to the underlying Java libraries (i.e. ScheduledThreadPoolExecutor and friends). The syntax is like this:
You can also shedule events at specific times: