Skip to content

Instantly share code, notes, and snippets.

@aiba
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aiba/046fe12ca2109aa7bd15 to your computer and use it in GitHub Desktop.
Save aiba/046fe12ca2109aa7bd15 to your computer and use it in GitHub Desktop.
(require '[clojure.contrib.seq-utils :refer [indexed]])
(import (java.util.concurrent Executors ConcurrentHashMap))
(defn pmapn [n f coll]
(let [executor (Executors/newFixedThreadPool n)
total (count coll)
results (ConcurrentHashMap. total 0.75 n)]
;; enqueue work
(doseq [[i x] (indexed coll)]
(.execute executor #(.put results i (f x))))
;; wait for threads to complete
(loop []
(if (< (.size results) total)
(do (Thread/sleep 100) (recur))
(map #(.get results %) (range total))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment