On Dec 26, 11:42 pm, Alex Osborne <[email protected]> wrote:
> justinhj <[email protected]> writes:
> > I tried passing *out* to my thread function and then binding it to
> > *out* in the thread, and this works but then makes the threads execute
> > one at a time, and I'm presuming that is because my use of *out* in
> > the binding block is blocking for the other threads which use it.
>
> That doesn't sound right: binding itself never blocks. Can you post
> some example code where you see this behaviour?
>
> As I understand it binding conveyance should happen automatically in the
> upcoming 1.3 release (for futures and agents) but in 1.2 you can use
> bound-fn, or for better performance use binding explicitly as you
> suggested.
>
> (future-call (bound-fn [] (println "log...")))
>
> (send some-agent (bound-fn [x] (println "log...") (inc x)))
This is the code I've written so far
(defn sleeper-thread [out id t]
"Sleep for time T ms"
(binding [*out* out]
(printf "%d sleeping for time %d\n" id t)
(Thread/sleep t)
(printf "%d slept\n" id)))
(defn test-threads [n out]
(dotimes [x n]
(.start (Thread. (#(sleeper-thread %1 %2 %3) out x (+ 2000 (rand-
int 5000)))))))
And the output is
0 sleeping for time 5480
0 slept
1 sleeping for time 6739
1 slept
2 sleeping for time 5444
2 slept
3 sleeping for time 3087
3 slept
4 sleeping for time 6753
4 slept
5 sleeping for time 3489
5 slept
6 sleeping for time 5864
6 slept
7 sleeping for time 5523
7 slept
8 sleeping for time 5659
8 slept
9 sleeping for time 5052
9 slept
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en