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

What is the purpose of await() in CountDownLatch?

I have the following program, where I am using java.util.concurrent.CountDownLatch and without using await() method it's working fine. I am new to concurrency and want to know the purpose of await(). In CyclicBarrier I can understand why await() is…
8
votes
3 answers

Test to force a race condition using CowntDownLatch causes java.lang.IllegalMonitorStateException

I tried to create a test where I tried to force a race condition (or at least to increase the probability of its occurrence) and I've used a CountDownLatch. The problem is that I get a java.lang.IllegalMonitorStateException at my…
user978080
6
votes
1 answer

Multiple calls to CountDownLatch.await(int) with timeout

I use a CountDownLatch for waiting for a certain event from another component (running in a different thread). The following approach would fit the semantics of my software, but I'm not sure whether it works as a I…
Thomas Calc
  • 2,994
  • 3
  • 30
  • 56
5
votes
2 answers

CountDownLatch in Android locking thread

I've just started playing around with CountDownLatch in my Android app. Currently I am trying to make two Volley requests to my api, and wait until the data has been retrieved and stored before continuing with thread execution. This is a sample of…
barnacle.m
  • 2,070
  • 3
  • 38
  • 82
5
votes
3 answers

Best way to wait for retrofit2 to finish before continuing async

I realize similar questions have been asked but I am new to android and find the answers a bit confusing since they are in a slightly different context. I have looked at CountDownLatch aswell as using Threads and am not sure which method to…
5
votes
2 answers

Java CountDownLatch waits for the timeout value when the thread.run() counting down the latch gets an exception

I am using a CountDownLatch to handle two Java threads. My class structure is as follows: MainClass.java ThreadOne.java ThreadTwo.java MainClass: CountDownLatch latch = new CountDownLatch(2); Thread thread = new Thread(new…
NPrasad
  • 108
  • 1
  • 6
5
votes
1 answer

Thread blocked on CountDownLatch await() when count is 0

While analyzing the logs of one of the production environments, I saw a thread in "WAITING" state on a countdownlatch await() ...sun.misc.Unsafe.park(Native Method) ...java.util.concurrent.locks.LockSupport.park(Unknown…
Andy Dufresne
  • 6,022
  • 7
  • 63
  • 113
4
votes
1 answer

Elegant way to run parallel threads in Spring 4

I am developing an API. This API needs to do 2 DB queries to get the result. I tried following strategies: Used callable as return type in Controller. Created 2 threads in Service (use Callable and CoundownLatch) to run 2 queries parallel and…
duy
  • 579
  • 5
  • 16
4
votes
2 answers

Countdownlatch and further synchronisation

Supposedly I have the following class definition, when one thread wants to set a for multiple (potentially) waiting threads: public class A {     private int a;     private CountDownLatch gate;     public A(int a) {         a = 1;         gate =…
Bober02
  • 15,034
  • 31
  • 92
  • 178
4
votes
2 answers

Implementing pingpong game correctly

I'm requested in an assignment to implement pingpong game that called "ping" and "pong" correctly (meaning, no pong before ping) 10 times. Meaning, the final output in the console should be: "ping!(1)", "pong!(1)", "ping!(2)", "pong!(2)" etc. The…
DanielY
  • 1,141
  • 30
  • 58
4
votes
1 answer

Can Java's CountDownLatch be used from a passive class rather than a thread?

So I am working on this program that simulates a day at work and each worker is its own thread. I'm trying to implement meetings where the workers attend meetings but the meetings do not start until everyone that is supposed to be at the meeting has…
grg-n-sox
  • 717
  • 2
  • 5
  • 25
3
votes
1 answer

CountDownLatch in Java need additional synchronization?

Let's say I have the code below: public class CountDownLatchExample { // Note : Exception handling ommited void main(){ CountDownLatch startSignal = new CountDownLatch(1); CountDownLatch doneSignal = new…
3
votes
0 answers

doWork() for WorkManager called multiple times for OneTimeWorkRequest

I have just started to explore WorkManager in my app. My app will mostly be offline, so all the data is stored locally using room db. As soon as the device gets connected to a network I want to sync local data to server and then get the latest data…
3
votes
3 answers

java concurrency: lightweight nonblocking semaphore?

I have a situation where I have a callback that I want to execute once. For the sake of argument let's say it looks like this: final X once = new X(1); Runnable r = new Runnable() { @Override public void run() { if (once.use()) …
Jason S
  • 184,598
  • 164
  • 608
  • 970
3
votes
2 answers

Android Worker execute the job multiple times

I use the following code to schedule a background job execute every 15 mins. WorkManager workManager = WorkManager.getInstance(); PeriodicWorkRequest ulpBackup; ulpBackup = new PeriodicWorkRequest .Builder(Ulp.class, 15, TimeUnit.MINUTES) …
1
2
3
11 12