class MyThreadPool

require ‘./my_thread_pool/version’

Constants

VERSION

Public Class Methods

new(size = 2) click to toggle source
# File lib/my_thread_pool.rb, line 4
def initialize(size = 2)
  @queue = Queue.new
  @threads = Array.new(size) do
    Thread.new do
      id = (rand * 100).to_i
      while task = @queue.pop and !task.nil?
        task.call
      end
    end
  end
end

Public Instance Methods

join() click to toggle source
# File lib/my_thread_pool.rb, line 20
def join
  @threads.size.times { @queue.push nil }
  @threads.map(&:join)
end
perform(&block) click to toggle source
# File lib/my_thread_pool.rb, line 16
def perform(&block)
  @queue.push block
end