在Eclipse / CounterClockWise中使用线程时,输出将发送到控制台而不是REPL

时间:2013-03-04 09:06:21

标签: eclipse clojure counterclockwise

我在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窗口,我该如何实现?

1 个答案:

答案 0 :(得分:6)

这是因为*out*未绑定到新线程中的REPL编写器。您可以手动绑定它:

(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"))
相关问题