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