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

How can we execute Java threads in a sequence?

Here is a scenario, when the output I am expecting from below code is First Function Second Function Main Execution Completed! But the problem here is the run method that prints "Second Function" is never getting the value of volatile flag to true…
0
votes
0 answers

C++: implementation of simple latch arrive_and_wait using std::condition_variable

Could you please help me to implement a simple latch arrive_and_wait using std::condition_variable? My project uses C++17 and so I can not use C++20 std::latch. So, I am trying to implement a simple latch myself. Here is the simplest example. The…
S.V
  • 2,149
  • 2
  • 18
  • 41
0
votes
1 answer

How to wait until for few callable tasks complete and then execute

I want to execute some processors in two threads.. few of them are independent and could run anytime but few of them have dependency.. whenver order of execution reaches to that processor i want to check whether all previous callable tasks are…
0
votes
0 answers

Java atomicity / proper use of synchronizers & atomicity to achieve a simple use case

Working in a very simple use case challenge( just something self inflicted) Use case: A collection, that just writes data to it. The caveat is : once it reaches a certain size (eg:25 size), the data currently in it should be flushed out( say written…
0
votes
0 answers

Simple Function to latch_setbit_resetbit

I have 32-bit MCU. The port uses 32 digital IO functionality.STM32F469/479 Series. I am looking for a sample code to Latch a particular bit/particular pin in PORTA Set/RESET function particular bit In port A. Can some one guide me on how it can be…
AMPS
  • 319
  • 3
  • 11
0
votes
0 answers

Deadlock in GCC 12.2.0 and std::latch

The following code often freezes when I compile/run it using GCC 12.2.0 #include #include #include #include int main() { for(unsigned k = 0; k != 10; k++) { std::latch sync{2}; auto task = [&sync]()…
Koosha
  • 1,492
  • 7
  • 19
0
votes
1 answer

Start Threads same time and dispute global variables

I'm trying to understand how to implement Threads disputing global variables. In my implementation I created 2 variables and I want 4 Threds (e.g.) to dispute it by decrementing. The first problem is that the way I implemented to consume will always…
billy_ta
  • 3
  • 1
0
votes
0 answers

sending multiple types of Runnable Tasks to Executorservice

Suppose we have tow types of Task classes( First and Second )implementing Runnable. both of them have an access to a shared object (syncObj) which is supposed to be the Synchronization Object. we want to use just one Thread pool with any number of…
Ali.Nemat
  • 39
  • 3
  • 5
0
votes
1 answer

CountDownLatch.await() hangs up

Could anyone explain me why CountDownLatch on method await is hanging up although I execute countDown in separated thread? This test never ends and what is interesting ThreadPoolTaskExecutor doesn't execute code where I want to execute countDown…
lolcio
  • 346
  • 5
  • 12
0
votes
1 answer

How to make main thread wait to complete UncaughtExceptionHandler execution

I have the following mutlithreaded code. I want the LatchCode.doStuff() to wait until UncaughtExceptionHandler handler completes it work, but it wasn't. How could I make the main thread to wait for it. I need to propagate the exception to parent…
0
votes
1 answer

using countDownLatch.await() to make sure result is delivered

Full source code can be found here : https://github.com/alirezaeiii/SavingGoals-Cache This is LocalDataSource class : @Singleton class QapitalLocalDataSource @Inject constructor( private val goalsDao: GoalsDao ) : LocalDataSource { override…
Ali
  • 9,800
  • 19
  • 72
  • 152
0
votes
0 answers

Changing thread pool size at runtime in Java ThreadPoolExecutor

I am new to core Java programming and I am currently working on a project related to multi-threading. We have an asynchronous service that multiple clients call. We have a separate consumer for each client that consumes and processes requests from…
0
votes
0 answers

Is there a Java built-in class that blocks till an event happens and can be interrupted to throw an exception?

I'm using CountDownLatches to wait for a specific even but I need to be able to abort its await() method and throw an exception right away. It's not nearly a challenge but I'm wondering if Java has something out of the box to do that for me because…
Muhammad Gelbana
  • 3,890
  • 3
  • 43
  • 81
0
votes
1 answer

CountdownLatch Demo program .Not waiting for coutdown latch to get over

In this program why All countdownlatch over message printed in between .Although it should wait for all the countdown latch to get over .As an extra Thread is started in the main method but that should be handled as cdl.countDown() method is called…
0
votes
1 answer

Why does main thread start execution when its method calls latch.await()

I have a main Thread which calls an assertion. Inside the assertion there is another method which is being called and that method uses latch.await() . Now the Main thread dosen't wait for the assertion to be completed and moves forward in the…