-
-
Save anonymous/e1d931a83084893e8349 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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