Class: Kudzu::ThreadPool
- Inherits:
-
Object
- Object
- Kudzu::ThreadPool
- Defined in:
- lib/kudzu/thread_pool.rb
Instance Method Summary collapse
-
#initialize(size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #shutdown ⇒ Object
- #start(&block) ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(size) ⇒ ThreadPool
Returns a new instance of ThreadPool.
3 4 5 6 7 |
# File 'lib/kudzu/thread_pool.rb', line 3 def initialize(size) @size = size @queue = Queue.new @threads = [] end |
Instance Method Details
#shutdown ⇒ Object
20 21 22 23 |
# File 'lib/kudzu/thread_pool.rb', line 20 def shutdown @threads.each { |t| t.kill } @threads = [] end |
#start(&block) ⇒ Object
9 10 11 |
# File 'lib/kudzu/thread_pool.rb', line 9 def start(&block) @threads = 1.upto(@size).map { create_thread(&block) } end |
#wait ⇒ Object
13 14 15 16 17 18 |
# File 'lib/kudzu/thread_pool.rb', line 13 def wait until @queue.num_waiting == @threads.select { |t| t.alive? }.size Thread.pass sleep 1 end end |