Skip to content

Instantly share code, notes, and snippets.

@aphyr
Created July 29, 2012 18:30
Show Gist options
  • Save aphyr/3200862 to your computer and use it in GitHub Desktop.
Save aphyr/3200862 to your computer and use it in GitHub Desktop.
Clojure message passing test
(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