Skip to content

Instantly share code, notes, and snippets.

@Keita-N
Created January 15, 2015 12:03
Show Gist options
  • Save Keita-N/a016a03a4d734ac39fef to your computer and use it in GitHub Desktop.
Save Keita-N/a016a03a4d734ac39fef to your computer and use it in GitHub Desktop.
require 'thread'
queue = Queue.new
(1..1000).each { |i| queue.push i }
threads = []
concurrency = 5
concurrency.times do |thread_id|
threads << Thread.new(thread_id) { |id|
until queue.empty?
print "Thread (#{id}) pop #{queue.pop}\n"
sleep rand(10) / 10
end
}
end
threads.each { |th| th.join }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment