From: Paul Tsung Date: 2006-06-04T06:13:33+09:00 Subject: SizedQueue 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. I wrote a test script using SizedQueue to limit the number of threads. require 'thread' @items = %w{item1 item2 item3 item4 item5} @queue = SizedQueue.new(1) @thread = [] @nap = 3 def testthread(item) @thread << Thread.new(item) do | i | puts Thread.list puts "#{i} will sleep for #{@nap} seconds" sleep(@nap) puts "#{i} woke up" @queue.deq end end @items.each do | item | @queue.enq( testthread(item) ) end @thread.each { | th | th.join } I set the SizedQueue max to one but the output I get is this. # # item1 will sleep for 3 seconds # # # item2 will sleep for 3 seconds item2 woke up item1 woke up # # # item3 will sleep for 3 seconds # # # item4 will sleep for 3 seconds item4 woke up item3 woke up # # item5 will sleep for 3 seconds item5 woke up The question is why are there three threads running concurrently from the output. I know one is the main thread. Then I expect only one other thread to be running. Is this right or am I doing something wrong? Thanks for any suggestions or input. -- Posted via http://www.ruby-forum.com/.