From: Sean O'Halpin Date: 2009-04-17T16:20:43+09:00 Subject: Re: Multi Threading On Fri, Apr 17, 2009 at 7:55 AM, Robert Klemme wrote: > > Much better though is this approach > > require 'thread' > > # use library class > queue = Queue.new > > # _first_ start threads > # does not really matter but if filling > # the queue takes time work can > # begin immediately > threads = (1..5).map do >  label = Thread.current.to_s.freeze > >  Thread.new queue do |q| >    until ( job = q.deq ) == :terminate >      puts "Thread ID: #{label}.Job  started" >      puts job >      puts "Thread ID: #{label}.Job  complete" >    end >  end > end > > # fill queues > 500.times do |a| >  queue.enq a > end > > # "close" queues > threads.size.times { queue.enq :terminate } > > # wait for termination > threads.each do |th| >  th.join > end > > Cheers > > robert > > -- > remember.guy do |as, often| as.you_can - without end > http://blog.rubybestpractices.com/ > > Minor nitpick - these lines should be reversed: > label = Thread.current.to_s.freeze > Thread.new queue do |q| i.e. > Thread.new queue do |q| > label = Thread.current.to_s.freeze Regards, Sean