Skip to content

Instantly share code, notes, and snippets.

@pesterhazy
Created April 19, 2016 19:10
Show Gist options
  • Save pesterhazy/c41786690ee0f8a3e5a21f88fa4a486a to your computer and use it in GitHub Desktop.
Save pesterhazy/c41786690ee0f8a3e5a21f88fa4a486a to your computer and use it in GitHub Desktop.
(ns background
(:require [clojure.tools.logging :as log])
(:import [java.util.concurrent]))
(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