Class: Concurrent::JavaCountDownLatch
- Inherits:
-
Object
- Object
- Concurrent::JavaCountDownLatch
- Defined in:
- lib/concurrent/atomic/count_down_latch.rb
Overview
A synchronization object that allows one thread to wait on multiple other threads. The thread that will wait creates a ‘CountDownLatch` and sets the initial value (normally equal to the number of other threads). The initiating thread passes the latch to the other threads then waits for the other threads by calling the `#wait` method. Each of the other threads calls `#count_down` when done with its work. When the latch counter reaches zero the waiting thread is unblocked and continues with its work. A `CountDownLatch` can be used only once. Its value cannot be reset.
Instance Method Summary collapse
-
#count ⇒ Fixnum
The current value of the counter.
-
#count_down ⇒ Object
Signal the latch to decrement the counter.
-
#initialize(count) ⇒ JavaCountDownLatch
constructor
Create a new ‘CountDownLatch` with the initial `count`.
-
#wait(timeout = nil) ⇒ Boolean
Block on the latch until the counter reaches zero or until ‘timeout` is reached.
Constructor Details
#initialize(count) ⇒ JavaCountDownLatch
Create a new ‘CountDownLatch` with the initial `count`.
78 79 80 81 82 83 |
# File 'lib/concurrent/atomic/count_down_latch.rb', line 78 def initialize(count) unless count.is_a?(Fixnum) && count >= 0 raise ArgumentError.new('count must be in integer greater than or equal zero') end @latch = java.util.concurrent.CountDownLatch.new(count) end |
Instance Method Details
#count ⇒ Fixnum
The current value of the counter.
101 102 103 |
# File 'lib/concurrent/atomic/count_down_latch.rb', line 101 def count @latch.getCount end |
#count_down ⇒ Object
Signal the latch to decrement the counter. Will signal all blocked threads when the ‘count` reaches zero.
96 97 98 |
# File 'lib/concurrent/atomic/count_down_latch.rb', line 96 def count_down @latch.countDown end |
#wait(timeout = nil) ⇒ Boolean
Block on the latch until the counter reaches zero or until ‘timeout` is reached.
86 87 88 89 90 91 92 93 |
# File 'lib/concurrent/atomic/count_down_latch.rb', line 86 def wait(timeout = nil) if timeout.nil? @latch.await true else @latch.await(1000 * timeout, java.util.concurrent.TimeUnit::MILLISECONDS) end end |