Joel VanderWerf
7/14/2008 5:26:00 PM
Michael Boutros wrote:
> require 'enumerator'
>
> holder = []
>
> array = (1..10).to_a
> puts array.inspect
>
> array.each_slice(3) do |group|
> group.each do |number|
> @thread = Thread.new do
> puts "Starting #{number}...\n"
> sleep(5)
> holder << number
> end
> end
> end
>
> @thread.join
#join is definitely a good idea, because otherwise (as you observed) the
main thread will exit before the others have finished, but you are
overwriting the @thread variable on each iteration through the loop.
The usual idiom for this is something like:
threads = array.map { Thread.new {...} }
threads.each {|th| th.join}
--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407