Skip to content

Commit 1212db4

Browse files
committed
ASYNC-271: Modified dispatch to work off of Executor instances instead of ExecutorService. Then, removed the use of newVTPertask... and implemented a reified Executor that defers to startVirtualThread instead. Modified relevant documentation to match changes.
1 parent 8799f84 commit 1212db4

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/main/clojure/clojure/core/async.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ go block threads - use Thread.setDefaultUncaughtExceptionHandler()
1717
to catch and handle.
1818
1919
Use the Java system property `clojure.core.async.executor-factory`
20-
to specify a function that will provide ExecutorServices for
20+
to specify a function that will provide an Executor instance for
2121
application-wide use by core.async in lieu of its defaults. The
2222
property value should name a fully qualified var. The function
2323
will be passed a keyword indicating the context of use of the
24-
executor, and should return either an ExecutorService, or nil to
24+
executor, and should return either an Executor, or nil to
2525
use the default. Results per keyword will be cached and used for
2626
the remainder of the application. Possible context arguments are:
2727
@@ -36,7 +36,7 @@ flow/process
3636
3737
:core-async-dispatch - used for completion fn handling (e.g. in put!
3838
and take!, as well as go block IOC thunk processing) throughout
39-
core.async. If not supplied the ExecutorService for :io will be
39+
core.async. If not supplied, the Executor for :io will be
4040
used instead.
4141
4242
The set of contexts may grow in the future so the function should

src/main/clojure/clojure/core/async/impl/dispatch.clj

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
(ns ^{:skip-wiki true}
1010
clojure.core.async.impl.dispatch
11-
(:import [java.util.concurrent Executors ExecutorService ThreadFactory]))
11+
(:import [java.util.concurrent Executors Executor ExecutorService ThreadFactory]))
1212

1313
(set! *warn-on-reflection* true)
1414

@@ -120,8 +120,9 @@
120120
(defn- make-io-executor
121121
[]
122122
(if vthreads-available-and-allowed?
123-
(-> (.getDeclaredMethod Executors "newVirtualThreadPerTaskExecutor" (make-array Class 0))
124-
(.invoke nil (make-array Class 0)))
123+
(reify Executor
124+
(execute [_ r]
125+
(Thread/startVirtualThread r)))
125126
(make-ctp-named :io)))
126127

127128
(defn ^:private create-default-executor
@@ -132,12 +133,12 @@
132133
:mixed (make-ctp-named :mixed)))
133134

134135
(def executor-for
135-
"Given a workload tag, returns an ExecutorService instance and memoizes the result. By
136+
"Given a workload tag, returns an Executor instance and memoizes the result. By
136137
default, core.async will defer to a user factory (if provided via sys prop) or construct
137-
a specialized ExecutorService instance for each tag :io, :compute, and :mixed. When
138+
a specialized Executor instance for each tag :io, :compute, and :mixed. When
138139
given the tag :core-async-dispatch it will default to the executor service for :io."
139140
(memoize
140-
(fn ^ExecutorService [workload]
141+
(fn ^Executor [workload]
141142
(let [sysprop-factory (when-let [esf (System/getProperty "clojure.core.async.executor-factory")]
142143
(requiring-resolve (symbol esf)))
143144
sp-exec (and sysprop-factory (sysprop-factory workload))]
@@ -148,7 +149,7 @@
148149

149150
(defn exec
150151
[^Runnable r workload]
151-
(let [^ExecutorService e (executor-for workload)]
152+
(let [^Executor e (executor-for workload)]
152153
(.execute e r)))
153154

154155
(defn run

0 commit comments

Comments
 (0)