2013-03-04 27 views
5

我嘗試這個代碼從this指南:的輸出被髮送在Eclipse使用線程時代替控制檯REPL的/逆時針

(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窗口,我該怎麼做到這一點?

回答

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")) 
相關問題