Why does CountDownLatch.getCount () return a long but not int?

I looked at the code, all int is the parameter passed to the CountDownLatch constructor is int, the variable in Sync is int, the return type Sync.getCount () is int. But CountDownLatch.getCount () returns long? I wonder why.

+7
source share
4 answers

I do not know if you will find a sufficient answer to this question if someone who developed this API does not answer, but he says that it is intended for "debugging and testing."

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

Futureproofing?

Just because CountDownLatch (int) is the only constructor that exists does not mean you could not add CountDownLatch (long) in Java 8 if anyone ever comes up with a use for this kind of thing.

In any case, the value is only indicative, not reliable.

+2
source

I would suggest, because int is used to store unsigned int, counting from 0 to 2 ** 32-1. Although you can store an unsigned int in int, when performing calculations with it it is much easier to push the value to a long one, which may well correspond to this range.

+1
source

I have another idea: simple supervision.

When you go to the source code, you will find that the implementation uses the internal Sync class. Sync also has getCount (), and it returns an int. In other words: the entire implementation is based on int; and only one external getter uses a long one. I think this makes no sense.

+1
source

All Articles