Class: Zm::Utils::ThreadPool
- Inherits:
-
Object
- Object
- Zm::Utils::ThreadPool
- Defined in:
- lib/zm/utils/thread_pool.rb
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #clear! ⇒ Object
- #finish!(result, number) ⇒ Object
- #finish? ⇒ Boolean
-
#initialize(size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #jobs_size ⇒ Object
-
#run! ⇒ Object
run threads and perform jobs from queue.
-
#schedule(*args, &block) ⇒ Object
add a job to queue.
- #stop ⇒ Object
Constructor Details
#initialize(size) ⇒ ThreadPool
Returns a new instance of ThreadPool.
6 7 8 9 10 11 12 13 |
# File 'lib/zm/utils/thread_pool.rb', line 6 def initialize(size) @finish = false @result = nil @number = nil @size = size @jobs = Queue.new @pool = init_pool end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
4 5 6 |
# File 'lib/zm/utils/thread_pool.rb', line 4 def number @number end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
4 5 6 |
# File 'lib/zm/utils/thread_pool.rb', line 4 def result @result end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
4 5 6 |
# File 'lib/zm/utils/thread_pool.rb', line 4 def size @size end |
Instance Method Details
#clear! ⇒ Object
35 36 37 |
# File 'lib/zm/utils/thread_pool.rb', line 35 def clear! @jobs = [] end |
#finish!(result, number) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/zm/utils/thread_pool.rb', line 19 def finish!(result, number) @finish = true @result = result @number = number @jobs = [[ Proc.new {}, nil]] end |
#finish? ⇒ Boolean
15 16 17 |
# File 'lib/zm/utils/thread_pool.rb', line 15 def finish? @finish end |
#jobs_size ⇒ Object
26 27 28 |
# File 'lib/zm/utils/thread_pool.rb', line 26 def jobs_size @jobs.size end |
#run! ⇒ Object
run threads and perform jobs from queue
40 41 42 43 44 45 46 |
# File 'lib/zm/utils/thread_pool.rb', line 40 def run! @size.times do schedule { throw :exit } end @pool.map(&:join) stop end |
#schedule(*args, &block) ⇒ Object
add a job to queue
31 32 33 |
# File 'lib/zm/utils/thread_pool.rb', line 31 def schedule(*args, &block) @jobs << [block, args] end |
#stop ⇒ Object
48 49 50 51 52 53 |
# File 'lib/zm/utils/thread_pool.rb', line 48 def stop @jobs.close @pool.each(&:exit) @pool.clear true end |