class ThreadPool

Attributes

queue[R]

Public Class Methods

new(tasks, thread_limit, thread_object=Thread) click to toggle source
# File lib/thread_pool.rb, line 4
def initialize(tasks, thread_limit, thread_object=Thread)
  @queue         = populate_queue(tasks)
  @thread_limit  = thread_limit
  @thread_object = thread_object
end

Public Instance Methods

execute() click to toggle source
# File lib/thread_pool.rb, line 10
def execute
  (0...@thread_limit).map do
    create_worker
  end.each(&:join)
end

Private Instance Methods

create_worker() click to toggle source
# File lib/thread_pool.rb, line 27
def create_worker
  @thread_object.new do
    begin
      execute_tasks
    rescue ThreadError
    end
  end
end
execute_tasks() click to toggle source
# File lib/thread_pool.rb, line 36
def execute_tasks
  while task = @queue.pop(true)
    task.call
  end
end
populate_queue(tasks) click to toggle source
# File lib/thread_pool.rb, line 18
def populate_queue(tasks)
  _queue = Queue.new
  tasks.each do |task|
    _queue.push(task)
  end

  _queue
end