站长资源脚本专栏
Ruby实现生产者和消费者代码分享
简介#ruby实现生产者和消费者代码require 'thread'queue = Queue.newconsumers = Thread.new do5.times do |i|obj = queue.popprint "consumer :#{i}\n"sleep(rand(0.05))end
#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
上一篇:Ruby一行代码实现的快速排序
下一篇:Ruby常用文件操作代码实例