Created
July 21, 2013 13:48
-
-
Save anonymous/6048626 to your computer and use it in GitHub Desktop.
scheduling a function via concurrent Executors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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