Output is sent to console instead of REPL when using threads in Eclipse/CounterClockWise

拈花ヽ惹草 提交于 2019-12-10 13:56:25

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!