我剛剛瀏覽了關於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。