Multiple Workers One Sink
suggest changeWe want to gather data created by multiple workers.
First we create a Queue:
sink = Queue.new
Then 16 workers each generate a random number and push it into the sink:
(1..16).to_a.map do
Thread.new do
sink << rand(1..100)
end
end.map(&:join)
After the workers have finished, drain the queue into an array:
data = [].tap { |a| a << sink.pop until sink.empty? }
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents