Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Created April 18, 2015 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindlychung/8770cc28cc76250071cd to your computer and use it in GitHub Desktop.
Save kindlychung/8770cc28cc76250071cd to your computer and use it in GitHub Desktop.
(ns cina.ref-types)
(import '(java.util.concurrent Executors))
(def ^:dynamic *pool* (Executors/newFixedThreadPool
(+ 2 (.availableProcessors (Runtime/getRuntime)))))
(defn dothreads [f & {thread-count :threads exec-count :times :or {thread-count 1 exec-count 1}}]
(dotimes [t thread-count]
(.submit *pool* #(dotimes [_ exec-count] (f)))))
(def ticks (atom 0))
(defn tick [] (swap! ticks inc))
(dothreads tick :threads 1000 :times 100)
@ticks ;=> 100000
(def uid (ref 0))
(def used-id (ref []))
(defn use-id []
(dosync
(commute uid inc)
(commute used-id conj @uid)))
(dothreads use-id :threads 10 :times 10)
@used-id
@kindlychung
Copy link
Author

=> [1 2 3 4 5 6 1 7 1 1 1 12 1 14 13 1 1 16 13 12 14 22 23 22 25 14 1 28 28 29 31 32 24 34 34 36 37 37 31 32 41 42 43 32 44 32 36 37 38 50 51 52 52 53 49 54 49 50 50 51 53 62 63 64 65 66 67 63 63 64 64 64 64 64 75 76 76 78 79 80 80 82 83 83 84 86 86 79 89 82 89 92 93 93 95 93 97 98 98 100]

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