Last active
April 14, 2017 17:44
-
-
Save neektza/ba05d4f8dac290d585b7097fa355bf59 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'thread' | |
require_relative 'add_one_task' | |
scheduled = []; performed = []; producers = []; consumers = [] | |
5.times do | |
producers << Thread.new do | |
scheduled.push AddOneTask.new | |
end | |
end | |
5.times do | |
consumers << Thread.new do | |
loop do | |
task = scheduled.pop | |
performed << task.perform unless task.nil? | |
break if scheduled.empty? | |
end | |
end | |
end | |
(producers + consumers).each { |t| t.join } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment