Skip to content

Instantly share code, notes, and snippets.

@zugnush
Created May 4, 2023 19:48
Show Gist options
  • Save zugnush/0da091b2aed3c2a22e06d6c54202b680 to your computer and use it in GitHub Desktop.
Save zugnush/0da091b2aed3c2a22e06d6c54202b680 to your computer and use it in GitHub Desktop.
updated ref example
(import [java.util.concurrent Executors])
(defn test-stm [nitems nthreads niters]
(let [refs (map ref (replicate nitems 0))
pool (. Executors (newFixedThreadPool nthreads))
tasks (map (fn [t]
(fn []
(dotimes [n niters]
(sync nil
(doseq [r refs]
(ref-set r (+ @r t)))))))
(map inc (range nthreads)))]
(. pool (invokeAll tasks))
(. pool (shutdown))
(map deref refs)))
(test-stm 10 10 10000)
;; => (550000 550000 550000 550000 550000 550000 550000 550000 550000 550000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment