Created
February 12, 2013 10:03
-
-
Save dennispaagman/4761326 to your computer and use it in GitHub Desktop.
Multi threaded ruby script. Based on http://stackoverflow.com/questions/6558828/thread-and-queue
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
# Encoding: UTF-8 | |
require 'thread' | |
queue = Queue.new | |
threads = [] | |
20.times do | |
threads << Thread.new do | |
# loop until there are no more things to do | |
until queue.empty? | |
# pop with the non-blocking flag set, this raises | |
# an exception if the queue is empty, in which case | |
# work_unit will be set to nil | |
work_unit = queue.pop(true) rescue nil | |
if work_unit | |
begin | |
# .... | |
rescue Exception => e | |
puts "-- Found Exception: #{e}" | |
end | |
end | |
end | |
end | |
end | |
threads.each { |t| t.join } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment