简单教程
提交运行
代码编辑器:
# !/usr/bin/ruby -w # -*- encoding:utf-8 -*- # filename: main.rb # author: 简单教程(www.twle.cn) # Copyright © 2015-2065 www.twle.cn. All rights reserved. require "thread" puts "SizedQueue 测试" queue = Queue.new producer = Thread.new do 5.times do |i| sleep rand(i) # 让线程睡眠一段时间 queue << i puts "#{i} produced" end end consumer = Thread.new do 5.times do |i| value = queue.pop sleep rand(i/2) puts "consumed #{value}" end end consumer.join
运行结果: