class FutureProof::ThreadPool
ThreadPool
could be used to schedule and group threads.
Public Class Methods
new(size)
click to toggle source
Initializes a new thread pool.
@params [FixNum] size the size of the pool thread.
# File lib/future_proof/thread_pool.rb, line 10 def initialize(size) @size = size @threads = [] @queue = Queue.new @values = FutureProof::FutureQueue.new end
Public Instance Methods
finalize()
click to toggle source
Flags that after all pool jobs are processed thread pool should stop the reactor.
# File lib/future_proof/thread_pool.rb, line 50 def finalize @size.times { @queue.push :END_OF_WORK } end
finalize!()
click to toggle source
Commands to remove all pool tasks and finishes the execution after all running tasks are completed.
# File lib/future_proof/thread_pool.rb, line 70 def finalize! @queue.clear finalize end
perform()
click to toggle source
Starts execution of the thread pool.
@note Can be restarted after finalization.
# File lib/future_proof/thread_pool.rb, line 32 def perform unless @threads.any? { |t| t.alive? } @values.start! @size.times do @threads << Thread.new do while job = @queue.pop if job == :END_OF_WORK break else @values.push *job[1], &job[0] end end end end end end
submit(*args, &block)
click to toggle source
Submits a task to a thread pool.
@param [Array<Object>] *args job arguments.
@example
thread_pool.submit(25, 2) { |a, b| a ** b }
@note Does not start the execution until perform
is called.
# File lib/future_proof/thread_pool.rb, line 25 def submit(*args, &block) @queue.push [block, args] end
values()
click to toggle source
Calls wait
and returns array of all calculated values.
@return [FutureProof::FutureArray] instance of FutureArray
with all values.
# File lib/future_proof/thread_pool.rb, line 63 def values wait @values.stop! @values.values end
wait()
click to toggle source
Calls finalize
and blocks programm flow until all jobs are processed.
# File lib/future_proof/thread_pool.rb, line 55 def wait finalize @threads.map &:join end