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.
8 9 10 11 12 13 14 15 |
# File 'lib/zm/utils/thread_pool.rb', line 8 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.
6 7 8 |
# File 'lib/zm/utils/thread_pool.rb', line 6 def number @number end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
6 7 8 |
# File 'lib/zm/utils/thread_pool.rb', line 6 def result @result end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
6 7 8 |
# File 'lib/zm/utils/thread_pool.rb', line 6 def size @size end |
Instance Method Details
#clear! ⇒ Object
37 38 39 |
# File 'lib/zm/utils/thread_pool.rb', line 37 def clear! @jobs = [] end |
#finish!(result, number) ⇒ Object
21 22 23 24 25 26 |
# File 'lib/zm/utils/thread_pool.rb', line 21 def finish!(result, number) @finish = true @result = result @number = number @jobs = [[proc {}, nil]] end |
#finish? ⇒ Boolean
17 18 19 |
# File 'lib/zm/utils/thread_pool.rb', line 17 def finish? @finish end |
#jobs_size ⇒ Object
28 29 30 |
# File 'lib/zm/utils/thread_pool.rb', line 28 def jobs_size @jobs.size end |
#run! ⇒ Object
run threads and perform jobs from queue
42 43 44 45 46 47 48 |
# File 'lib/zm/utils/thread_pool.rb', line 42 def run! @size.times do schedule { throw :exit } end @pool.map(&:join) stop end |
#schedule(*args, &block) ⇒ Object
add a job to queue
33 34 35 |
# File 'lib/zm/utils/thread_pool.rb', line 33 def schedule(*args, &block) @jobs << [block, args] end |
#stop ⇒ Object
50 51 52 53 54 55 |
# File 'lib/zm/utils/thread_pool.rb', line 50 def stop @jobs.close @pool.each(&:exit) @pool.clear true end |