Last active
October 10, 2021 06:42
-
-
Save ik5/3e7184174cf0bad231b931aaab131eba to your computer and use it in GitHub Desktop.
example on how to do threadpool in ruby
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' | |
queue = Queue.new | |
threads = [] | |
1.step(1000) do |i| | |
queue << i | |
end | |
8.times do | |
threads << Thread.new do | |
until queue.empty? | |
work_unit = queue.pop(true) rescue nil | |
if work_unit | |
puts work_unit | |
sleep 5 | |
end | |
end | |
end | |
end | |
threads.map(&:join) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment