in Education by
This question already has answers here: CountDownLatch vs. Semaphore (7 answers) Closed 5 years ago. CountDownLatch in java is a high-level synchronization utility which is used to prevent a particular thread to start processing until all threads are ready. But, Semaphore can totally do the same thing. So, what's the merit of CountDownLatch? One more question: If CountDownLatch do have some merits, Why It was designed to used only once? I think it's easy to add a set method to reset the count. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Semantically, they're different; and that matters, because it makes your code easier to read. When I see a Semaphore, I immediately start thinking "a limited amount of a shared resource." When I see a CountDownLatch, I immediately start thinking "a bunch of threads waiting for the 'go!' signal." If you give me the former in code that actually needs the latter, it's confusing. In this sense, a Semaphore being used as a CountDownLatch is a bit like a garden-path sentence; while technically correct, it leads people astray and confuses them. In terms of more pragmatic uses, a CountDownLatch is just simpler if that's all you need. Simpler is better! As for reusing a CountDownLatch, that would complicate its usage. For instance, let's say you're trying to queue up threads A, B, and C for some work. You have them await on the latch, and then you release it. Then you reset it, presumably to queue up threads D, E and F for some other work. But what happens if (due to a race condition), thread B hasn't actually been released from the first latch yet? What if it hadn't even gotten to the await() call yet? Do you close the gate on it, and tell it to wait with D, E and F for the second opening? That might even cause a deadlock, if the second opening depends on work that B is supposed to be doing! I had the same questions you did about resetting when I first read about CountDownLatch. But in practice, I've rarely even wanted to reset one; each unit of "wait then go" (A-B-C, then D-E-F) naturally lends itself to creating its own CountDownLatch to go along with it, and things stay nice and simple.

Related questions

0 votes
    This question already has answers here: CountDownLatch vs. Semaphore (7 answers) Closed 5 years ago. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: CountDownLatch vs. Semaphore (7 answers) Closed 5 years ago. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: CountDownLatch vs. Semaphore (7 answers) Closed 5 years ago. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    What's the main point of difference between human & machine intelligence? (a) human perceive everything as a pattern ... ) human have sense organs Please answer the above question....
asked Sep 21, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: converting date format (5 answers) Closed 5 years ago. I have a ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I am trying to use design support library. Gradle dependencies are as follows dependencies { compile 'com. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use design support library. Gradle dependencies are as follows dependencies { compile 'com. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Saving path in String (4 answers) Closed 5 years ago. I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Saving path in String (4 answers) Closed 5 years ago. I'm ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to deal with "java.lang.OutOfMemoryError: Java heap space" error ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 14, 2022 in Education by JackTerrance
0 votes
    I am trying to use design support library. Gradle dependencies are as follows dependencies { compile 'com. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: How to reference constants in EL? (12 answers) Closed 6 years ago ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Closed 9 years ago. Possible Duplicate: Java equivalent to #region ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Can I catch multiple Java exceptions in the same catch clause? (10 ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    I have a method that's about ten lines of code. I want to create more methods that do exactly ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 23, 2022 in Education by JackTerrance
...