Ruby线程实现经典的生产者消费者问题

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

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|  
        value = queue.pop  
        sleep rand(i/2)  
        puts "consumed #{value}"  
    end  
end  
  
consumer.join  #等待consumer线程的输出完成再关闭此进程