Skip to content

Instantly share code, notes, and snippets.

@neektza
Last active April 14, 2017 17:44
Show Gist options
  • Save neektza/ba05d4f8dac290d585b7097fa355bf59 to your computer and use it in GitHub Desktop.
Save neektza/ba05d4f8dac290d585b7097fa355bf59 to your computer and use it in GitHub Desktop.
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