Questions tagged [countdownlatch]

A countdown latch is a synchronization primitive that allows one or more threads to wait until a certain number of operations are completed on other threads.

A countdown latch is a synchronization primitive that allows one or more threads to wait until a certain number of operations are completed on other threads.

The primitive keeps an internal counter initialized with the number of operations to complete. When a working thread signals the completion of an operation, the internal counter is decremented. When this counter reaches zero, the waiting threads are allowed to continue execution.

Resources

Documentation of Java's CountdownLatch class

166 questions
210
votes
13 answers

How is CountDownLatch used in Java Multithreading?

Can someone help me to understand what Java CountDownLatch is and when to use it? I don't have a very clear idea of how this program works. As I understand all three threads start at once and each Thread will call CountDownLatch after 3000ms. So…
amal
  • 3,470
  • 10
  • 29
  • 43
180
votes
14 answers

Java concurrency: Countdown latch vs Cyclic barrier

I was reading through the java.util.concurrent API, and found that CountDownLatch: A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. CyclicBarrier: A…
daydreamer
  • 87,243
  • 191
  • 450
  • 722
112
votes
7 answers

CountDownLatch vs. Semaphore

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;…
finnw
  • 47,861
  • 24
  • 143
  • 221
36
votes
5 answers

What is the difference between join and CountDownLatch?

When waiting for other threads to finish, we can use either join or CountdownLatch. What are the pros and cons of using either of those two mechanisms?
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
30
votes
6 answers

Flexible CountDownLatch?

I have encountered a problem twice now whereby a producer thread produces N work items, submits them to an ExecutorService and then needs to wait until all N items have been processed. Caveats N is not known in advance. If it were I would simply…
Adamski
  • 54,009
  • 15
  • 113
  • 152
28
votes
3 answers

Java support for three different concurrency models

I am going through different concurrency model in multi-threading environment (http://tutorials.jenkov.com/java-concurrency/concurrency-models.html) The article highlights about three concurrency models: Parallel Workers The first concurrency…
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
24
votes
2 answers

Is there a C# equivalent to Java's CountDownLatch?

Is there a C# equivalent to Java's CountDownLatch?
Kiril
  • 39,672
  • 31
  • 167
  • 226
22
votes
9 answers

Resettable CountdownLatch

I need something which is directly equivalent to CountDownLatch, but is resettable (remaining thread-safe!). I can't use classic synchronisation constructs as they simply don't work in this situation (complex locking issues). At the moment, I'm…
Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
15
votes
3 answers

Put one thread to sleep until a condition is resolved in another thread

Here are two chunks of code that accomplish (what I think is) the same thing. I basically am trying to learn how to use Java 1.5's concurrency to get away from Thread.sleep(long). The first example uses ReentrantLock, and the second example uses…
10
votes
3 answers

How to wait for a thread that spawns it's own thread?

I'm trying to test a method that does it's work in a separate thread, simplified it's like this: public void methodToTest() { Thread thread = new Thread() { @Override public void run() { Clazz.i = 2; } …
Aequitas
  • 2,205
  • 1
  • 25
  • 51
10
votes
1 answer

How to return a value using CompletableFuture

I created an example, i want to know how can I return a value using the CompletableFuture ? I also changed the CompletableFuture exeFutureList to be CompletableFuture exeFutureList but eclipse always suggest to set it back to…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
9
votes
2 answers

Is CountDownLatch affected by spurious wakeups?

Concurrency management mechanisms such as wait/notify and lock/condition seem to be affected by spurious wakeups. Developers cater for those unexpected wakeups by re-checking that the condition has indeed changed. When it comes to CountDownLatch,…
Fidel
  • 7,027
  • 11
  • 57
  • 81
9
votes
2 answers

Java: is CountDownLatch threadsafe

In the docs for CountDownLatch I see something like: public void run() { try { startSignal.await(); doWork(); doneSignal.countDown(); } catch (InterruptedException ex) {} // return; } Here startSignal and…
treecoder
  • 43,129
  • 22
  • 67
  • 91
8
votes
4 answers

why CountDownLatch.getCount() returns a long but not an int?

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
8
votes
1 answer

Angular 2 multiple countdown pipe

I'm looking to create angular 2/4 countdown pipe. Of course I can make individual countdowns, but how do I create multiple ones? I want the following input: Countdown will count here for example:
maria
  • 207
  • 5
  • 22
  • 56
1
2 3
11 12