Skip to content

Instantly share code, notes, and snippets.

@DayoOliyide
Last active July 30, 2022 15:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DayoOliyide/f353b15563675120e408b6b9f504628a to your computer and use it in GitHub Desktop.
Save DayoOliyide/f353b15563675120e408b6b9f504628a to your computer and use it in GitHub Desktop.
Get all thread information in clojure
(defn print-threads [& {:keys [headers pre-fn]
:or {pre-fn identity}}]
(let [thread-set (keys (Thread/getAllStackTraces))
thread-data (mapv bean thread-set)
headers (or headers (-> thread-data first keys))]
(clojure.pprint/print-table headers (pre-fn thread-data))))
(defn print-threads-str [& args]
(with-out-str (apply print-threads args)))
(comment
;;print all properties for all threads
(print-threads)
;;print name,state,alive,daemon properties for all threads
(print-threads :headers [:name :state :alive :daemon])
;;print name,state,alive,daemon properties for non-daemon threads
(print-threads :headers [:name :state :alive :daemon] :pre-fn (partial filter #(false? (:daemon %))))
;;log name,state,alive,daemon properties for non-daemon threads
(log/debug (print-threads-str :headers [:name :state :alive :daemon] :pre-fn (partial filter #(false? (:daemon %)))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment