问题
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?
回答1:
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"))
来源:https://stackoverflow.com/questions/15197914/output-is-sent-to-console-instead-of-repl-when-using-threads-in-eclipse-counterc