Skip to content

Instantly share code, notes, and snippets.

@agrison
Created October 14, 2019 13:41
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 agrison/31defbcd4a3cd91837d77ebdc8bab613 to your computer and use it in GitHub Desktop.
Save agrison/31defbcd4a3cd91837d77ebdc8bab613 to your computer and use it in GitHub Desktop.
runp! runs a given function on a given collection in parallel.
(defn runp!
"Runs the function `f` in parallel on the given collection `coll`.
It will use (by default) the same numbers of threads as there are cores available on the system.
You can set a specific number of threads using `:threads` waits maximum `:timeout` milliseconds."
([f coll {:keys [threads timeout] :or {threads (.availableProcessors (Runtime/getRuntime))}}]
(let [pool (java.util.concurrent.Executors/newFixedThreadPool threads)
tasks (map (fn [e] (fn [] (f e))) coll)]
(try
(doseq [future (.invokeAll pool tasks)]
(.get future))
(catch InterruptedException e
(.shutdownNow pool))
(finally
(.shutdown pool)
(when timeout
(when-not (.awaitTermination pool timeout java.util.concurrent.TimeUnit/MILLISECONDS)
(.shutdownNow pool))))))))
(defn runp!
"Runs the function `f` in parallel on the given collection `coll`.
It will use (by default) the same numbers of threads as there are cores available on the system.
You can set a specific number of threads using `:threads`."
([f coll]
(runp! f coll (.availableProcessors (Runtime/getRuntime))))
([f coll threads]
(let [pool (java.util.concurrent.Executors/newFixedThreadPool threads)
tasks (map (fn [e] (fn [] (f e))) coll)]
(try
(doseq [future (.invokeAll pool tasks)]
(.get future))
(finally
(.shutdown pool))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment