Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 61991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:24:21+00:00 2026-05-10T18:24:21+00:00

Is there any advantage of using java.util.concurrent.CountdownLatch instead of java.util.concurrent.Semaphore ? As far as

  • 0

Is there any advantage of using

java.util.concurrent.CountdownLatch

instead of

java.util.concurrent.Semaphore?

As far as I can tell the following fragments are almost equivalent:

1. Semaphore

final Semaphore sem = new Semaphore(0); for (int i = 0; i < num_threads; ++ i) {   Thread t = new Thread() {     public void run()     {       try       {         doStuff();       }       finally       {         sem.release();       }     }   };   t.start(); }  sem.acquire(num_threads); 

2: CountDownLatch

final CountDownLatch latch = new CountDownLatch(num_threads); for (int i = 0; i < num_threads; ++ i) {   Thread t = new Thread() {     public void run()     {       try       {         doStuff();       }       finally       {         latch.countDown();       }     }   };   t.start(); }  latch.await(); 

Except that in case #2 the latch cannot be reused and more importantly you need to know in advance how many threads will be created (or wait until they are all started before creating the latch.)

So in what situation might the latch be preferable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:24:21+00:00Added an answer on May 10, 2026 at 6:24 pm

    CountDownLatch is frequently used for the exact opposite of your example. Generally, you would have many threads blocking on await() that would all start simultaneously when the countown reached zero.

    final CountDownLatch countdown = new CountDownLatch(1);  for (int i = 0; i < 10; ++ i) {    Thread racecar = new Thread() {           public void run() {          countdown.await(); //all threads waiting          System.out.println("Vroom!");       }    };    racecar.start(); } System.out.println("Go"); countdown.countDown();   //all threads start now! 

    You could also use this as an MPI-style "barrier" that causes all threads to wait for other threads to catch up to a certain point before proceeding.

    final CountDownLatch countdown = new CountDownLatch(num_thread);  for (int i = 0; i < num_thread; ++ i) {    Thread t= new Thread() {           public void run() {          doSomething();          countdown.countDown();          System.out.printf("Waiting on %d other threads.",countdown.getCount());          countdown.await();     //waits until everyone reaches this point          finish();       }    };    t.start(); } 

    That all said, the CountDownLatch can safely be used in the manner you’ve shown in your example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 105k
  • Answers 105k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unless the SWIGged C++ code is specifically set up to… May 11, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer I'm assuming that because you mentioned a versioning scheme, that… May 11, 2026 at 8:41 pm
  • Editorial Team
    Editorial Team added an answer You need to create the following constructor in SurfaceView for… May 11, 2026 at 8:41 pm

Related Questions

I have a large collection of data in an excel file (and csv files).
This is a follow up question to Is there a basic Java Set implementation
It's been at least 5 years since I worked with Java, and back then,
I am considering starting a project which is used to generate code in Java

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.