class Bow::ThreadPool
Attributes
max_threads[RW]
queue[RW]
Public Class Methods
new(max_threads = 4) { |self| ... }
click to toggle source
# File lib/bow/thread_pool.rb, line 7 def initialize(max_threads = 4) @queue = [] @threads = [] @history = [] @max_threads = max_threads return unless block_given? yield(self) run end
Public Instance Methods
add(&task)
click to toggle source
# File lib/bow/thread_pool.rb, line 17 def add(&task) add_task(task) end
from_enumerable(enumerable, &block)
click to toggle source
# File lib/bow/thread_pool.rb, line 29 def from_enumerable(enumerable, &block) enumerable.each do |params| task = build_task(block, params) add_task(task) end end
run()
click to toggle source
# File lib/bow/thread_pool.rb, line 21 def run @queue.each do |task| wait run_task(task) end wait_for_all end
Private Instance Methods
add_task(task)
click to toggle source
# File lib/bow/thread_pool.rb, line 47 def add_task(task) @queue << task end
build_task(handler, params)
click to toggle source
# File lib/bow/thread_pool.rb, line 43 def build_task(handler, params) -> { handler.call(params) } end
run_task(task)
click to toggle source
# File lib/bow/thread_pool.rb, line 38 def run_task(task) thr = Thread.new(&task) @threads << thr end
wait()
click to toggle source
# File lib/bow/thread_pool.rb, line 51 def wait return if @threads.count < @max_threads until @threads.count < @max_threads active_threads = @threads.select(&:alive?) @history += (@threads - active_threads) @threads = active_threads sleep 0.001 end end
wait_for_all()
click to toggle source
# File lib/bow/thread_pool.rb, line 61 def wait_for_all return if @threads.empty? # @threads.map!(&:run) @threads.each(&:join) end