Gorsal wrote: > I'm trying to redirect the input i receive from a BufferedInputStream > to the repl. I'm trying something like this: > > (defmacro with-thread [nm & body] > `(let [thread# (Thread. (fn [] (do ~...@body)))] > (if ~nm (.setName thread# ~nm)) > (.start thread#) > thread#)) > > (defn redirect-stream [nm stream function] > (with-thread nm > (loop [the-char nil] > (if (and the-char (not (= the-char -1))) (jprint (char the- > char))) > (let [value (function stream the-char)] > (if (not value) > (recur (.read stream) > (and (> (.available stream) 0) > (.read stream)) > )))))) > However, this raises the CPU to about 50 percent. This is due to the > infinite recursion, I'm assuming?
Yes, it's because you're tight looping, checking to see if data is available as fast as possible. A quick and dirty hack would be to put in a sleep to slow it down a bit. (if (> (.available stream) 0) (.read stream) (Thread/sleep 100)) A better option would be to use NIO and a selectable channel to block waiting for data availability: http://java.sun.com/j2se/1.4.2/docs/api/java/nio/channels/Selector.html Then you can use wakeup() on the Selector to interrupt it. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---