Class: ForwardProxy::ThreadPool
- Inherits:
-
Object
- Object
- ForwardProxy::ThreadPool
- Defined in:
- lib/forward_proxy/thread_pool.rb
Instance Attribute Summary collapse
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
Instance Method Summary collapse
-
#initialize(size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #schedule(*args, &block) ⇒ Object
- #start ⇒ Object
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
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
3 4 5 |
# File 'lib/forward_proxy/thread_pool.rb', line 3 def queue @queue end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
3 4 5 |
# File 'lib/forward_proxy/thread_pool.rb', line 3 def size @size end |
#threads ⇒ Object (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
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 |
#start ⇒ Object
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 |