Skip to content

Instantly share code, notes, and snippets.

/ruby.rb Secret

Created October 2, 2015 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e1d931a83084893e8349 to your computer and use it in GitHub Desktop.
Save anonymous/e1d931a83084893e8349 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'thread'
worktodo = Queue.new
numthreads = 25
Thread.new {
for i in 0..50
puts "QUEUE WORK #{i}"
worktodo << "#{i}"
sleep 0.1
end
for i in 0..numthreads
worktodo << "END"
end
}
threads = []
for i in 0..numthreads
threads << Thread.new {
work = worktodo.pop
while work!="END" do
puts "POP #{work}"
sleep rand*5
puts "DONE #{work}"
end
}
sleep 1
end
# Join all threads
puts "Waiting for threads to end"
threads.each { |thr| thr.join }
puts "End of program"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment