justinhj <justi...@gmail.com> writes:

> On Dec 26, 11:42 pm, Alex Osborne <a...@meshy.org> wrote:

> (defn test-threads [n out]
>   (dotimes [x n]
>     (.start (Thread. (#(sleeper-thread %1 %2 %3) out x (+ 2000 (rand-
> int 5000)))))))

Ah.  The problem is here.  You're calling that lambda in the main thread
and then passing the return value of sleeper-thread to (Thread.) (which
doesn't make much sense).

Try this:

(defn test-threads [n out]
  (dotimes [x n]
    (.start (Thread. #(sleeper-thread out x (+ 2000 (rand-int 5000)))))))

Or even:

(defn test-threads [n out]
  (dotimes [x n]
    (future (sleeper-thread out x (+ 2000 (rand-int 5000))))))

Or forgetting about passing the out argument around:

(defn test-threads [n]
  (dotimes [x n]
    (let [out *out*]
      (future 
        (binding [*out* out]
          (sleeper-thread x (+ 2000 (rand-int 5000))))))

You could turn that into a macro:

(defmacro future-with-out [& body]
  `(let [out# *out*]
     (future (binding [*out* out#] ~...@body)))

And then use it like:

(defn test-threads [n]
  (dotimes [x n]
    (future-with-out (sleeper-thread x (+ 2000 (rand-int 5000))))))

-- 
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

Reply via email to