Created
April 4, 2013 13:58
-
-
Save pbalduino/5310581 to your computer and use it in GitHub Desktop.
Future and promise in Clojure
This file contains hidden or 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
(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...")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"