我从 this 尝试了这段代码指导:
(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"))
当我执行它时,部分输出显示在 REPL 中,另一部分显示在控制台中(因为我通常隐藏它,因为我不使用它,所以它会弹出)。
我想将所有输出发送到 REPL 窗口,我该如何实现?
最佳答案
这是因为*out*
未绑定(bind)到新线程中的 REPL 编写器。您可以手动绑定(bind)它:
(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"))
关于eclipse - 在 Eclipse/CounterClockWise 中使用线程时,输出被发送到控制台而不是 REPL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15197914/