示例代码: 代码如下: require “thread” puts “ProAndCon” queue = Queue.new #用队列Queue实现线程同步 producer = Thread.new do 10.times do |i| sleep rand(i) # 让线程睡眠一段时间 queue << i puts “#{i} produced” end end consumer = Thread.new do 10.times do |i|