Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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)
(.put out (inc value))
(recur in out m))
(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)
(recur a m)))
(let [x (atom 1)
w1 (future (bump x m))
w2 (future (bump x m))]
(prn @x))))
(defn -main
"I don't do a whole lot."
[test & args]
(case test
"queue" (queue-test)
"atom" (atom-test))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment