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
3
votes
0 answers

Wait x seconds or until sql query returns results

I'm trying to implement a method that continuously run a SQL query until it returns results or fails after x seconds. Currently my method uses CountDownLatch: final CountDownLatch done = new CountDownLatch(1); new Thread(new Runnable() { …
user1607549
  • 1,499
  • 3
  • 13
  • 17
3
votes
2 answers

How to reduce the time delay to reach run method of Runnable class using ExecutorService Java

I was trying to implement a real-time executing application in which a button click event will assign a task to a Thread , which will call a midi Method to play some music. The music has to be started immediately when button is clicked with a small…
3
votes
2 answers

How to get results from the CompletableFuture

each of "CompletableFuture.runAsync" mentioned in the code below does some calculations, an i want to get the results each time i call "CompletableFuture.runAsync". or in other words, i want each of "future0,future1,future2,future3" to contain the…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
3
votes
3 answers

Why CountDownLatch in java cannot change its state again?

In Java once a CountdownLatch reaches its state = 0, it cannot change it, so it remains open forever. I wonder why implementors don't allow to reuse CountDownLatch?
Ivan Voroshilin
  • 5,233
  • 3
  • 32
  • 61
3
votes
2 answers

Is there a way to declare a method as a new Thread in Java

I have a program which is listening for random numbers. It is hooked up to a publisher which gives me a number and a new count and every time I get an update, I'm storing the current count for that number in a HashMap. I also have an SSL server…
sally
  • 187
  • 2
  • 3
  • 11
2
votes
1 answer

Why C++ has introduced separate std::latch and std::barrier?

There are some similar questions on SO. But they only ask the question one way. std::latch has an advantage over std::barrier that unlike latter, former can be decremented by a participating thread more than once. std::barrier has an advantage over…
Sourav Kannantha B
  • 2,860
  • 1
  • 11
  • 35
2
votes
1 answer

CountDownLatch await method issue: doesn't throw on timeout

I am writing a simple program to demonstrate CountDownLatch. Here is my program: class Players implements Runnable { private int delay; private String name; private CountDownLatch latch; Players(int delay, String name,…
cr.7
  • 85
  • 1
  • 8
2
votes
1 answer

What happen if multiple threads will call await() method of CountDownLatch?

What happen if multiple threads will call await() method of CountDownLatch? We have for example 3 threads Thread1, Thread2, Thread3. Thread1 has running job. Thread2,Thread3 will call await method. What will be a result? Thread2 will be resumed at…
Dawid Macura
  • 193
  • 1
  • 9
2
votes
4 answers

Synchronise concurrent requests to share results of a slow operation

I have a Java UI service that has an API method that invokes an operation that's relatively slow (say ~30secs). The operation is parameterless, but it operates on external data that does change (relatively slowly) over time. It's not critical for…
jon hanson
  • 8,722
  • 2
  • 37
  • 61
2
votes
1 answer

Android webview blocking evaluateJavascript

Im trying to evaluateJavascript query with blocking function in my webview. Meaning function thread awaits result. However, evaluateJavascript and ValueCallback are both called on main thread, and main thread is paused awaiting for result, meaning…
Jonzi
  • 141
  • 1
  • 2
  • 13
2
votes
0 answers

Unit test a method with countdown latch

I am writing unit tests for a class which has a latch to block the execute method. Class structure is something like this : class Sample { private CountDownLatch latch; public void execute(String data) { latch = new…
eeedev
  • 149
  • 9
2
votes
1 answer

java Fork/Join pool, ExecutorService and CountDownLatch

We have three different multi threading techniques in java - Fork/Join pool, Executor Service & CountDownLatch Fork/Join pool (http://www.javacodegeeks.com/2011/02/java-forkjoin-parallel-programming.html) The Fork/Join framework is designed to make…
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
2
votes
2 answers

How to block reads whenever writes are happening on two flows?

I am trying to implement lock by which I want to avoid reads from happening whenever I am doing a write. My requirements are: Reads block until all two maps have been set for the first time. Now second time, If I am updating the maps, I can still…
john
  • 11,311
  • 40
  • 131
  • 251
2
votes
2 answers

Some threads gets stuck at semaphore.aquire() (threads/semaphore/countdownlatch)

I've created a small movie rental simulation program. Here's how it works: - The main thread lets the user input customers names Every customer typed in launches a new thread (the Customer Runnable) When 5 customers have been created, the rental…
asmb
  • 1,015
  • 2
  • 8
  • 11
2
votes
5 answers

How to explicitly release CountDownLatch

Is there any way to explicitly release CountDownLatch - means without do countDown(). E.g.: Let say I am waiting for 100 threads to do countDown(), but if something fails, I would like to release this latch without anymore waiting. I was thinking to…
To Kra
  • 3,344
  • 3
  • 38
  • 45
1 2
3
11 12