Skip to content

Instantly share code, notes, and snippets.

@andradefil
Created November 27, 2025 12:58
Show Gist options
  • Select an option

  • Save andradefil/f812dea9935f55af8cfd623317a495c6 to your computer and use it in GitHub Desktop.

Select an option

Save andradefil/f812dea9935f55af8cfd623317a495c6 to your computer and use it in GitHub Desktop.
Testing Thread Interruption
(require '[clojure.core.async :as a])
(a/<!! (a/thread (Thread/sleep 3000) "Foo"))
(def c (a/chan 2))
(def o (a/thread (a/<!! c)))
(a/>!! c "Foo")
(Thread/getAllStackTraces)
(defn list-threads
[]
(map (fn [t] {:thread t
:name (.getName t)}) (.keySet (Thread/getAllStackTraces))))
(defn interrupt-thread-by-name
[threads thread-name]
(doseq [th threads]
(if (= thread-name (:name th))
(do (.interrupt (:thread th))
true)
false)))
(defn interrupt-all-threads
[threads]
(doseq [th threads]
(.interrupt (:thread th))))
(def threads (list-threads))
(interrupt-thread-by-name threads "async-mixed-1")
(interrupt-thread-by-name threads "async-mixed-7")
(interrupt-thread-by-name threads "main")
(.interrupt (:thread (first threads)))
(.isInterrupted (:thread (first threads)))
(.stop (:thread (first threads)))
(interrupt-all-threads threads)
(interrupt-thread-by-name threads "main")
(.isInterrupted (Thread/currentThread))
(Thread/sleep 5000)
(Thread/sleep 3000)
(.isInterrupted (Thread/currentThread))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment