Class: ForwardProxy::ThreadPool

Inherits:
Object
  • Object
show all
Defined in:
lib/forward_proxy/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
# File 'lib/forward_proxy/thread_pool.rb', line 5

def initialize(size)
  @queue   = Queue.new
  @size    = size
  @threads = []
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



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

def queue
  @queue
end

#sizeObject (readonly)

Returns the value of attribute size.



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

def size
  @size
end

#threadsObject (readonly)

Returns the value of attribute threads.



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

def threads
  @threads
end

Instance Method Details

#schedule(*args, &block) ⇒ Object

Raises:

  • (Exception)


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

def schedule(*args, &block)
  raise Exception, "no threads" unless threads.any?(&:alive?)

  queue.push([block, args])
end

#startObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/forward_proxy/thread_pool.rb', line 11

def start
  size.times do
    thread = Thread.new do
      loop do
        job, args = queue.pop
        job.call(*args)
      end
    end

    threads.push(thread)
  end
end