Created
July 29, 2012 18:30
-
-
Save aphyr/3200862 to your computer and use it in GitHub Desktop.
Clojure message passing test
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
(ns messagepassing.core) | |
(import [java.util.concurrent LinkedTransferQueue]) | |
(def m 10000000) | |
(defn queue-test [] | |
(defn bounce [in out m] | |
(let [value (.take in)] | |
(if (< value m) | |
(do | |
(.put out (inc value)) | |
(recur in out m)) | |
value))) | |
(time | |
(let [q1 (LinkedTransferQueue. [1]) | |
q2 (LinkedTransferQueue.) | |
w1 (future (bounce q1 q2 m)) | |
w2 (future (bounce q2 q1 m))] | |
(prn @w2) | |
(future-cancel w1)))) | |
(defn atom-test [] | |
(defn bump [a m] | |
(when (> m (swap! a (fn [value] | |
(if (< value m) | |
(inc value) | |
value)))) | |
(recur a m))) | |
(time | |
(let [x (atom 1) | |
w1 (future (bump x m)) | |
w2 (future (bump x m))] | |
@w1 | |
@w2 | |
(prn @x)))) | |
(defn -main | |
"I don't do a whole lot." | |
[test & args] | |
(case test | |
"queue" (queue-test) | |
"atom" (atom-test)) | |
(shutdown-agents)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment