You are talking about a common question. Sometimes indeed some classes provided by JDK are functionally overlapping.
Another example is CountDownLatch
and Semaphore
. Basically you can replace one with another without much difference in functionality. This post: CountDownLatch vs. Semaphore may help in your confusion.
I think you can choose one which can make your code easier to understand. In your case, if you use a CyclicBarrier
it will never reset itself, so you would better use CountDownLatch
which makes the code easier to understand.
Take the constructor of Semaphore
for example: JDK provides two constructors
1 public Semaphore(int permits) {
2 sync = new NonfairSync(permits);
3 }
and
1 public Semaphore(int permits, boolean fair) {
2 sync = fair ? new FairSync(permits) : new NonfairSync(permits);
3 }
we can use Semaphore(3,false)
to replace Semaphore(3)
without any difference, both of them are constructing a Non-fair version semaphore. But why does JDK still provide two versions of constructors? Because choosing the right one from them can make the code easier to read and understand.