Ruby Language Queue One Source Multiple Workers

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

We want to process data in parallel.

Let's populate source with some data:

source = Queue.new
data = (1..100)
data.each { |e| source << e }

Then create some workers to process data:

(1..16).to_a.map do
  Thread.new do
    until source.empty?
      item = source.pop
      sleep 0.5
      puts "Processed: #{item}"
    end
  end
end.map(&:join)


Got any Ruby Language Question?