From: Robert Klemme Date: 2006-06-04T22:41:19+09:00 Subject: Re: SizedQueue 2006/6/3, Paul Tsung : > Does anyone know about threads and SizedQueue? > > I want to limit or control the number of threads created in a script. I > am not sure what is the best way to handle this. You probably haev a misconception on how a SizedQueue works. The limit on the queue ensures that at most n elements reside in the queue at any given point in time. You typically do that if the queue is filled faster than it is emptied. When the queue is full an enqueue operation blocks the calling thread. What you probably want is a leigth weight processing framework. The basic idea is to have a single queue (sized limited or not) and a fixed number of threads reading from the queue. A typical setting: require 'thread' MAX_THREADS = 10 QUEUE = Queue.new threads = (1..MAX_THREADS).map do |i| Thread.new(QUEUE) do |q| until ( q == ( task = q.deq ) ) # process task print "thread-", i, " ", task.inspect, "\n" end end end # generate tasks 1000.times {|i| QUEUE.enq i} # send terminators down the queue threads.size.times { QUEUE.enq QUEUE} # wait for threads to finish threads.each {|t| t.join} # eof HTH robert -- Have a look: http://www.flickr.com/photos/fussel-foto/