-
Notifications
You must be signed in to change notification settings - Fork 211
/
concurrent.clj
38 lines (34 loc) · 1.55 KB
/
concurrent.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
;; Copyright (c) Rich Hickey and contributors. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.
(ns ^{:skip-wiki true}
clojure.core.async.impl.concurrent
(:import [java.util.concurrent ThreadFactory]))
(set! *warn-on-reflection* true)
(defn counted-thread-factory
"Create a ThreadFactory that maintains a counter for naming Threads.
name-format specifies thread names - use %d to include counter
daemon is a flag for whether threads are daemons or not
opts is an options map:
init-fn - function to run when thread is created"
([name-format daemon]
(counted-thread-factory name-format daemon nil))
([name-format daemon {:keys [init-fn] :as opts}]
(let [counter (atom 0)]
(reify
ThreadFactory
(newThread [_this runnable]
(let [body (if init-fn
(fn [] (init-fn) (.run ^Runnable runnable))
runnable)
t (Thread. ^Runnable body)]
(doto t
(.setName (format name-format (swap! counter inc)))
(.setDaemon daemon))))))))
(defonce
^{:doc "Number of processors reported by the JVM"}
processors (.availableProcessors (Runtime/getRuntime)))