Class: InThreads::ThreadLimiter
- Inherits:
-
Object
- Object
- InThreads::ThreadLimiter
- Defined in:
- lib/in_threads/thread_limiter.rb
Overview
Use ThreadsWait to limit number of threads
Class Method Summary collapse
-
.limit(count, &block) ⇒ Object
Without block behaves as
new
With block yields it withself
and ensures running offinalize
.
Instance Method Summary collapse
-
#<<(thread) ⇒ Object
Add thread to
ThreadsWait
, wait for finishing of one thread if limit reached. -
#finalize ⇒ Object
Wait for waiting threads.
-
#initialize(count) ⇒ ThreadLimiter
constructor
Initialize with limit.
Constructor Details
#initialize(count) ⇒ ThreadLimiter
Initialize with limit
7 8 9 10 |
# File 'lib/in_threads/thread_limiter.rb', line 7 def initialize(count) @count = count @waiter = ThreadsWait.new end |
Class Method Details
.limit(count, &block) ⇒ Object
Without block behaves as new
With block yields it with self
and ensures running of finalize
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/in_threads/thread_limiter.rb', line 14 def self.limit(count, &block) limiter = new(count) if block begin yield limiter ensure limiter.finalize end else limiter end end |
Instance Method Details
#<<(thread) ⇒ Object
Add thread to ThreadsWait
, wait for finishing of one thread if limit reached
28 29 30 31 32 33 34 |
# File 'lib/in_threads/thread_limiter.rb', line 28 def <<(thread) if @waiter.threads.length + 1 >= @count @waiter.join(thread) else @waiter.join_nowait(thread) end end |
#finalize ⇒ Object
Wait for waiting threads
37 38 39 |
# File 'lib/in_threads/thread_limiter.rb', line 37 def finalize @waiter.all_waits end |