I tried this code from this guide:
(defn my-fn [ms]
(println "entered my-fn")
(Thread/sleep ms)
(println "leaving my-fn"))
(let [thread (Thread. #(my-fn 1))]
(.start thread)
(println "started thread")
(while (.isAlive thread)
(print ".")
(flush))
(println "thread stopped"))
When I execute it, part of the output shows up in the REPL, and the other part shows up in the console (which pops up since I usually have it hidden because I don't use it).
I want to send all the output to the REPL window, how can I achieve that?
It's because *out*
is not bound to REPL writer in new thread. You can bind it manually:
(let [thread (let [out *out*]
(Thread. #(binding [*out* out]
(my-fn 1))))]
(.start thread)
(println "started thread")
(while (.isAlive thread)
(print ".")
(flush))
(println "thread stopped"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With