Skip to content

Instantly share code, notes, and snippets.

@pbalduino
Created April 4, 2013 13:58
Show Gist options
  • Save pbalduino/5310581 to your computer and use it in GitHub Desktop.
Save pbalduino/5310581 to your computer and use it in GitHub Desktop.
Future and promise in Clojure
(defn start-thread [f]
(.start (Thread. f)))
(let
[sum (future (apply + (range 1e7)))
answer (promise)]
(println "0. Started...")
(start-thread
(fn []
(println "1. I'm in thread 1")
(println "1. Done: " @sum)
(println "1. Delivering answer")
(deliver answer 42)
(println "1. The thread is over")))
(start-thread
(fn []
(println "2. Waiting in thread 2")
(println (str "2. " @answer))
(println "2. unlocked")))
(println "0. And I'm just here..."))
@pbalduino
Copy link
Author

Output:

"0. Started..."
"1. I'm in thread 1"
"2. Waiting in thread 2"
"0. And I'm just here..."
nil
user=> "1. Done: 49999995000000"
"1. Delivering answer"
"1. The thread is over"
"2. 42"
"2. unlocked"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment