Class: Wikiranger::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/wikiranger/thread_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ ThreadPool

Returns a new instance of ThreadPool.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/wikiranger/thread_pool.rb', line 5

def initialize(size)
  @size = size.to_i
  @jobs = Queue.new
  @pool = Array.new(size) do
    Thread.new do
      catch(:exit) do
        loop do
          job, args = @jobs.pop
          job.call(*args)
        end
      end
    end
  end
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



3
4
5
# File 'lib/wikiranger/thread_pool.rb', line 3

def jobs
  @jobs
end

#poolObject (readonly)

Returns the value of attribute pool.



3
4
5
# File 'lib/wikiranger/thread_pool.rb', line 3

def pool
  @pool
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/wikiranger/thread_pool.rb', line 3

def size
  @size
end

Instance Method Details

#schedule(*args, &block) ⇒ Object



20
21
22
# File 'lib/wikiranger/thread_pool.rb', line 20

def schedule(*args, &block)
  @jobs << [block, args]
end

#shutdownObject



24
25
26
27
28
29
# File 'lib/wikiranger/thread_pool.rb', line 24

def shutdown
  @size.times do
    schedule { throw :exit }
  end
  @pool.map(&:join)
end