Last active
August 29, 2015 14:18
-
-
Save aiba/046fe12ca2109aa7bd15 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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