Fork me on GitHub
#core-async
<
2017-03-19
>
wei04:03:18

is there an idiomatic way to start a bunch of threads and wait for them all to finish?

noisesmith04:03:54

if you have them in a sequence, doseq works

+user=> (time (>/<!! (>/go (doseq [t (doall (repeatedly 10 #(>/thread (Thread/sleep 1000))))] (>/<! t)))))
"Elapsed time: 1006.401197 msecs"
nil

noisesmith04:03:36

without the doall, it would have taken 10 seconds instead of ~1

wei04:03:04

that’s what I was looking for- thanks!

wei04:03:24

wait, why is the doall necessary?

noisesmith04:03:41

:user=> (time (>/<!! (>/go (doseq [t (repeatedly 10 #(>/thread (Thread/sleep 1000)))] (>/<! t)))))
"Elapsed time: 10012.202001 msecs"
nil

noisesmith04:03:53

repeatedly is lazy, so each thread is not created until it is accessed

noisesmith04:03:15

this may or may not apply to your threads - just wanted to make sure to show that aspect

wei04:03:03

(time
 (>/<!!
  (>/go
    (doseq [t (dotimes [_ 10] #(>/thread (Thread/sleep 1000)))]
      (>/<! t)))))

noisesmith04:03:50

t will be a single nil

noisesmith04:03:29

you need something that actually returns the threads, dotimes will not - it returns nil