Skip to content

Instantly share code, notes, and snippets.

@Chouser
Created April 22, 2022 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Chouser/d07a4e883f637e2b6b19d979fe3688f3 to your computer and use it in GitHub Desktop.
Save Chouser/d07a4e883f637e2b6b19d979fe3688f3 to your computer and use it in GitHub Desktop.
core async reseting experiment
(ns io.chouser.experiment
(:require [clojure.core.async :as async]))
(defn bang []
(future
(dotimes [i 10]
(async/go
(print (str "start " i "\n")) (flush)
(Thread/sleep 20000)
(prn :done i)))))
(require '[clojure.core.async.impl.dispatch :as impl.d]
'[clojure.core.async.impl.exec.threadpool :as tp])
(import '(java.util.concurrent ThreadPoolExecutor))
(defn new-dispath-executor [_]
(delay (tp/thread-pool-executor #(.set ^ThreadLocal @#'impl.d/in-dispatch true))))
(defn reset-dispatch-executor []
(let [old-executor ^ThreadPoolExecutor @impl.d/executor
executor-svc-field (doto (.getDeclaredField (type old-executor) "executor_svc")
(.setAccessible true))
thread-pool-executor (.get executor-svc-field old-executor)]
(prn :largest (.getLargestPoolSize thread-pool-executor) :active (.getActiveCount thread-pool-executor))
(prn :shutdown (.shutdownNow thread-pool-executor))
(alter-var-root #'impl.d/executor new-dispath-executor)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment