Last active
March 28, 2017 22:24
-
-
Save pesterhazy/c805676657dbe997dfed9e8792e66d23 to your computer and use it in GitHub Desktop.
As replacement for clojure.core/future, java.util.concurrent.Executors
This file contains 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
(ns my.utils | |
(:require [clojure.tools.logging :as log]) | |
(:import java.util.concurrent.Executors)) | |
(defonce !executor (delay (java.util.concurrent.Executors/newCachedThreadPool))) | |
(defn background | |
"Calls the fn passed in a thread from a thread pool, returning immediately. | |
Unlike future, background does not swallow exceptions." | |
[f] | |
(.execute @!executor (fn [] (try | |
(f) | |
(catch Exception e | |
(log/error "Uncaught exception in background thread:" e)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment