Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 524
ChocoDeveloper Avatar asked Mar 04 '13 09:03

ChocoDeveloper


1 Answers

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"))
like image 169
mobyte Avatar answered Oct 07 '22 15:10

mobyte