#ruby實現(xiàn)生產(chǎn)者和消費者代碼
require 'thread'
queue = Queue.new
consumers = Thread.new do
5.times do |i|
obj = queue.pop
print "consumer :#{i}\n"
sleep(rand(0.05))
end
end
producters = Thread.new do
5.times do |i|
sleep(0.1)
print "producter : #{i}\n"
queue.push("Item #{i}")
end
end
producters.join
consumers.join