Skip to content

Instantly share code, notes, and snippets.

@gosuri
Created August 6, 2014 21:35
Show Gist options
  • Save gosuri/c9c9723b4155b070f858 to your computer and use it in GitHub Desktop.
Save gosuri/c9c9723b4155b070f858 to your computer and use it in GitHub Desktop.
require 'thread'
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) # simulate expense
puts "consumed #{value}"
end
end
consumer.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment