Code Monkey home page Code Monkey logo

virtual's Introduction

virtual

Clojars Project

Java 21 Virtual Thread Utilities

Motivation

Provide a more "clojure-y" syntax over Java 21's Virtual Thread Executor.

Usage

With "future-like" api:

(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<T>.
    (-> @(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:

(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      (<! messages)
          group-name (.getName (.getThreadGroup (Thread/currentThread)))]
      (>! 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 (<!! (take 10 processed))))

virtual's People

Contributors

bnert avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.