Skip to content

Instantly share code, notes, and snippets.

Created July 21, 2013 13:48
Show Gist options
  • Save anonymous/6048626 to your computer and use it in GitHub Desktop.
Save anonymous/6048626 to your computer and use it in GitHub Desktop.
scheduling a function via concurrent Executors
(import '(java.util.concurrent Executors TimeUnit))
(defn schedule-at-fixed-rate [se f init-delay period]
(.scheduleAtFixedRate
se
(fn [] (try (f) (catch Throwable e (.printStackTrace e))))
init-delay period
TimeUnit/SECONDS))
(defn cancel [se]
(.cancel se true))
(def s (Executors/newSingleThreadScheduledExecutor))
(def r (schedule-at-fixed-rate s #(prn "HI!") 5 5))
;; wait...
(Thread/sleep 10000)
(cancel r)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment