what's the correct syntax for new clojure?

0 views
Skip to first unread message

wubbie

unread,
Jan 3, 2009, 2:58:43 PM1/3/09
to Clojure


Hi,

What's the correct syntax for this code -- from clojure manual.
(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
(dosync
(doseq r refs
(alter r + 1 t))))))
(range nthreads))]
(doseq future (. pool (invokeAll tasks))
(. future (get)))
(. pool (shutdown))
(map deref refs)))

Stephen C. Gilardi

unread,
Jan 3, 2009, 4:11:16 PM1/3/09
to clo...@googlegroups.com
There's a correct, updated version here: http://clojure.org/concurrent_programming

(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]
(dosync
(doseq [r refs]
(alter r + 1 t))))))
(range nthreads))]

(doseq [future (.invokeAll pool tasks)]
(.get future))
(.shutdown pool)
(map deref refs)))

--Steve

> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> To unsubscribe from this group, send email to clojure+u...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/clojure?hl=en
> -~----------~----~----~----~------~----~------~--~---
>

Reply all
Reply to author
Forward
0 new messages