我刚刚浏览了关于Clojure并发性的各种文档,并且在网站(http://clojure.org/concurrent_programming)上介绍了该示例。了解Clojure并发性示例
(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需要[]?
非常感谢,
杜沙。
P.S.没有这第二个fn []我得到NullPointerException。