8

I looked into the code, everything is int -- the parameter passed to CountDownLatch constructor is int, the variable in Sync is int, the return type of Sync.getCount() is int. But CountDownLatch.getCount() returns a long? Wondering why.

DeepNightTwo
  • 4,809
  • 8
  • 46
  • 60

4 Answers4

3

I have a different idea: simple oversight.

When you turn to the source code, you will find that the implementation uses an internal class Sync. Sync has a getCount(), too - and that one returns int. In other words: the whole implementation is based on int; and only that one external getter is using long. I think there is absolutely no sense in that.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
3

I don't know if you will find a sufficient answer to that question unless someone who designed that API answers, but it does say it is for "debugging and testing".

public long getCount() {...} // just for debugging and testing
2

Futureproofing?

Just because CountDownLatch(int) is the only existing constructor, that doesn't mean you couldn't add CountDownLatch(long) in Java 8, if anyone ever comes up with a use for that kind of thing.

The value is only indicative, not reliable, anyway.

Bill Michell
  • 8,240
  • 3
  • 28
  • 33
1

I would guess it's because the int is being used to store an unsigned int, counting from 0 to 2**32-1. Although you can store an unsigned int in an int, when doing calculations with it, it is much easier to promote the value to a long, which can accommodate that range quite naturally.

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133