Skip to content

Instantly share code, notes, and snippets.

@jebberjeb
Last active December 25, 2015 04:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jebberjeb/6919854 to your computer and use it in GitHub Desktop.
Save jebberjeb/6919854 to your computer and use it in GitHub Desktop.
future-cancelled?
(ns test-app.core)
(defn do-it-slow!
[sleep-time]
(Thread/sleep sleep-time))
(defn mk-future
[sleep-time]
(future (do-it-slow! sleep-time)))
(defn kill-future
[f delay]
(future
(Thread/sleep delay)
(future-cancel f)))
(def f (mk-future 10000))
(try
(kill-future f 1000)
(println "derefing future, will be blocked")
(deref f)
(catch Exception e
(if (future-cancelled? f)
(println "future was cancelled while blocking at deref")
(throw e))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment