您现在的位置是:网站首页> 编程资料编程资料

Ruby实现生产者和消费者代码分享_ruby专题_

2023-05-26 427人已围观

简介 Ruby实现生产者和消费者代码分享_ruby专题_

 #ruby实现生产者和消费者代码 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

-六神源码网