Skip to content

Commit 4247c73

Browse files
committed
default count in CountDownLatch to 1
it's very common to use a CountDownLatch with a count of 1. Default `count` to 1 so that users can just call `new` and get a valid object with a count of 1.
1 parent b0fbc4e commit 4247c73

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

lib/concurrent/atomic/count_down_latch.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class MutexCountDownLatch
2020
# @param [Fixnum] count the initial count
2121
#
2222
# @raise [ArgumentError] if `count` is not an integer or is less than zero
23-
def initialize(count)
23+
def initialize(count = 1)
2424
unless count.is_a?(Fixnum) && count >= 0
2525
raise ArgumentError.new('count must be in integer greater than or equal zero')
2626
end
@@ -75,7 +75,7 @@ def count
7575
class JavaCountDownLatch
7676

7777
# @!macro count_down_latch_method_initialize
78-
def initialize(count)
78+
def initialize(count = 1)
7979
unless count.is_a?(Fixnum) && count >= 0
8080
raise ArgumentError.new('count must be in integer greater than or equal zero')
8181
end

spec/concurrent/atomic/count_down_latch_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
described_class.new('foo')
1919
}.to raise_error(ArgumentError)
2020
end
21+
22+
it 'defaults the count to 1' do
23+
latch = described_class.new
24+
expect(latch.count).to eq 1
25+
end
2126
end
2227

2328
describe '#count' do

0 commit comments

Comments
 (0)