/
executor.clj
57 lines (48 loc) · 1.6 KB
/
executor.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
(ns qbits.auspex.executor
(:import
(java.util.concurrent Executor
Executors
ForkJoinPool)))
(set! *warn-on-reflection* true)
(defn current-thread-executor
"Returns an executor that will run task in calling thread"
[]
(reify Executor
(execute [this r]
(.run r))))
(defn fork-join-executor
"Returns forkJoin commonPool Executor"
[]
(ForkJoinPool/commonPool))
(defn work-stealing-executor
"Creates a thread pool that maintains enough threads to support the
given parallelism level, and may use multiple queues to reduce
contention. Arity 1 will have parallelism = available processors"
([]
(Executors/newWorkStealingPool))
([parallelism]
(Executors/newWorkStealingPool (int parallelism))))
(defn cached-executor
"Creates a thread pool that creates new threads as needed, but will
reuse previously constructed threads when they are available"
[]
(Executors/newCachedThreadPool))
(defn single-executor
"Creates an Executor that uses a single worker thread operating off an
unbounded queue."
[]
(Executors/newSingleThreadExecutor))
(defn fixed-size-executor
"Returns a new fixed size executor of size `num-threads`."
[{:keys [num-threads thread-factory]
:or {thread-factory (Executors/defaultThreadFactory)}}]
(Executors/newFixedThreadPool (int num-threads)
thread-factory))
(defn clojure-future-executor
"Returns the thread pool used by clojure.core/future."
[]
(clojure.lang.Agent/soloExecutor))
(defn current-thread
"Returns current thread"
[]
(Thread/currentThread))