Class: TaskBag::Bag
- Inherits:
-
Object
- Object
- TaskBag::Bag
- Defined in:
- lib/taskbag/bag.rb
Instance Method Summary collapse
- #add(business) ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(task_class) ⇒ Bag
constructor
A new instance of Bag.
- #next ⇒ Object
- #open(nthreads) ⇒ Object
Constructor Details
#initialize(task_class) ⇒ Bag
3 4 5 6 |
# File 'lib/taskbag/bag.rb', line 3 def initialize(task_class) @tasks = [] @task_class = task_class end |
Instance Method Details
#add(business) ⇒ Object
16 17 18 |
# File 'lib/taskbag/bag.rb', line 16 def add(business) @tasks << business end |
#close ⇒ Object
20 21 22 23 24 |
# File 'lib/taskbag/bag.rb', line 20 def close loop { break unless @tasks.any? } @closed = true @threads.each{|t| t.join} end |
#closed? ⇒ Boolean
26 27 28 |
# File 'lib/taskbag/bag.rb', line 26 def closed? !!@closed end |
#next ⇒ Object
30 31 32 |
# File 'lib/taskbag/bag.rb', line 30 def next @tasks.pop end |
#open(nthreads) ⇒ Object
8 9 10 11 12 13 14 |
# File 'lib/taskbag/bag.rb', line 8 def open(nthreads) @closed = false bag = self @threads = nthreads.times.map do |w| Thread.new { @task_class.new(bag).start } end end |