Skip to content

Instantly share code, notes, and snippets.

@mwmitchell
Forked from anonymous/schedule_at_fixed_rate.clj
Last active December 20, 2015 01:19
Show Gist options
  • Save mwmitchell/6048627 to your computer and use it in GitHub Desktop.
Save mwmitchell/6048627 to your computer and use it in GitHub Desktop.
(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