【发布时间】:2011-01-06 02:46:01
【问题描述】:
我刚刚浏览了有关 Clojure 并发的各种文档,并在网站 (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)))
我了解它的作用和工作原理,但我不明白为什么需要第二个匿名函数 fn[]?
非常感谢,
杜莎。
附:如果没有这第二个 fn [],我会得到 NullPointerException。
【问题讨论】:
标签: concurrency clojure