Created
May 4, 2023 19:48
-
-
Save zugnush/0da091b2aed3c2a22e06d6c54202b680 to your computer and use it in GitHub Desktop.
updated ref example
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
(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