https://github.com/bnert-land/virtual
Java 21 Virtual Thread Utilities
https://github.com/bnert-land/virtual
clojure java-21 virtual-threads
Last synced: 3 months ago
JSON representation
Java 21 Virtual Thread Utilities
- Host: GitHub
- URL: https://github.com/bnert-land/virtual
- Owner: bnert-land
- License: epl-2.0
- Created: 2023-09-27T11:30:12.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-23T18:35:16.000Z (about 1 year ago)
- Last Synced: 2025-10-22T00:05:53.835Z (3 months ago)
- Topics: clojure, java-21, virtual-threads
- Language: Clojure
- Homepage:
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# virtual
[](https://clojars.org/land.bnert/virtual)
Java 21 Virtual Thread Utilities
## Motivation
Provide a more "clojure-y" syntax over Java 21's Virtual Thread Executor.
## Usage
With "future-like" api:
```clojure
(ns example.core
(:require
[virtual.core :refer [virtual virtual-let]]))
(defn -main [& _args]
; Will execute operation on a virtual thread.
; Result of the function is available via deref, given the underlying
; threading model returns a Future.
(-> @(virtual (slurp (io/file "some-file.edn"))) ; should "park" on io
edn/read-string)
(-> @(virtual-let [fname "config.edn"] (io/resource fname))
slurp
edn/read-string))
```
Monkey patching `clojure.core.async` to use virtual threads as the
thread executor:
```clojure
(ns example.core
(:require
[clojure.core.async :refer [go chan ! take]]
[virtual.async :refef [use-virtual-thread-executor!]]))
; Should be called, given this has a side effect of modifying the clojure.core.async
; thread pool executor.
(use-virtual-thread-executor!)
(def messages (chan))
(def processed (chan))
(def message-processor
(go-loop []
(let [value (! processed (into value {:processor-thread-group grou-name})))
(recur)))
(defn -main [& _args]
(dotimes [i 10]
(put! messages {:from i :to (dec i), :message "hello!"}))
(println (