App下載

ruby 在線工具

#!/usr/bin/ruby

require "thread"
puts "SizedQuee Test"

queue = Queue.new

producer = Thread.new do
     3.times do |i|
          sleep rand(i) # 讓線程睡眠一段時(shí)間
          queue << i
          puts "#{i} produced"
     end
end

consumer = Thread.new do
     3.times do |i|
          value = queue.pop
          sleep rand(i/2)
          puts "consumed #{value}"
     end
end

consumer.join
運(yùn)行結(jié)果