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
1
vote
1 answer

What is the better way to implement CountDownLatch in effective java 2nd item 72?

Effective Java Item 72 shows bad example of CountDownLatch implementation. But it doesn't show a right way to implement it. Do I have to use wait() and notify() instead of a while loop ? Can anyone suggest me a good example of this item ? Below is…
myoldgrandpa
  • 871
  • 7
  • 21
1
vote
2 answers

Java Using CountDownLatch to poll a method until a success response

I am trying to call a method multiple times every 60 seconds until a success response from the method which actually calls a rest end point on a different service. As of now I am using do while loop and using Thread.sleep(60000); to make the main…
Upen
  • 1,388
  • 1
  • 22
  • 49
1
vote
1 answer

Java 7 parallel execution is not improving performance of REST API using Countdown latch

I am writing a REST API in Java 7 to retrieve three items from database and send all three together to user as a response. When program runs in the sequential flow i.e. if I fetch one item then another then it is taking legitimate time for…
1
vote
1 answer

Concurrency primitives in Racket

I'm learning how to implement semaphores and countdown latches in Racket. Right now I'm using the Racket Docs, but I do not find them very useful. Would you recommend any websites, tutorials, textbooks, videos that can assist me throughout this…
Hope
  • 13
  • 5
1
vote
1 answer

Firebase Database getting Node Value

I want to get my wgID from my Firebase RealTime Database, it is located as a value of /WG/FlatNumber/ Now here is my Code: MainActivity: System.out.println("1"); dbContact.schreibeFlatObjekt(); System.out.println("7"); schreibeFlatObjekt: //wgID…
jochot
  • 167
  • 1
  • 13
1
vote
1 answer

High latency on increasing thread count

In below code DataGather = endDataGather - beginDataGather takes 1.7ms & time for service to respond = service_COMPLETED - service_REQUEST_SENT which vary from 20us to 200 us(since they are mocked dummy on same lan hence so low) now if i increase…
1
vote
1 answer

How to Unit Test a method using CountDownLatch?

I need to do the Unit Test of below code which is using countdownlatch. This is a test code only. I am using mockito thenAnswer and InvocationOnMask for mocking threads/callable. But I don't know how to initialize/mock or exit countdownlatch in unit…
ganesh
  • 41
  • 2
  • 4
1
vote
0 answers

CountDownLatch blocking Asnychronous Callback

I have the following construct in code public boolean do(){ final boolean[] returnValue = new boolean[1]; final CountDownLatch cdl = new CountDownLatch(1); event.addListener(new Listener() { @Override public void…
Adrian Jandl
  • 2,985
  • 4
  • 23
  • 30
1
vote
2 answers

Why main thread is waiting after starting first thread?

I was trying to understand the CountDownLatch usage, following is the code I am using here, DecrementRunnable.java package com.nirjhar.java.countdownlatchexample; import java.util.concurrent.CountDownLatch; public class…
1
vote
1 answer

Output of following java program on usage of CountDownLatch

what will be output of following java prgram - Will it always be as mentioned below or there is any corner case when it can be different - Expected output - task1 critical section completed task2 critical section completed Java program - import…
1
vote
1 answer

CountDownTimer not working at all

I am trying to run a CountDownTimer inside a Thread, but it just won't work.. So in the MainActivitys onCreate I start it on Button click like that: public void onClick(final View v) { Log.d("Main", "onClick"); …
Tobi
  • 924
  • 1
  • 10
  • 39
1
vote
1 answer

Problems about CountDownLatch await() method?

In my program CountDownLatch await () method will continue blocking program, CountDownLatch as it is written, Is a countdown latch, when the count to zero trigger three Thread execution, and prove that three when cdAnswer reduced to 0 Thread has…
wanghao
  • 271
  • 1
  • 7
  • 21
1
vote
3 answers

Thread execution continues even after exception in java

I have a skeleton of my threads in the code below. I have used a simple countdown latch. I am stuck in a situation where I have Thread 1 depending on thread 2 to complete. if there is no exception, the code runs properly. But chances of exception…
iamP
  • 307
  • 1
  • 3
  • 13
1
vote
1 answer

Getting an Auth Token from accountmanager using a CountdownLatch

I am trying to get the Authentication Token from an account in Android before I make my request to the server. I Am trying to control the flow with a CountdownLatch so that it waits until: a) A timeout (10s) b) We get the token private…
austin_ce
  • 1,063
  • 15
  • 28
1
vote
2 answers

CountdownLatch countDown method behavior after completion of task

Suppose i have a countdown latch of size 3 i.e. 3 threads are spawned off from parent thread. CountDownLatch latch = new CountDownLatch(3); latch.await(). Now there will be three threads which will be calling countDown after their respective task…
Timothy Drisdelle
  • 395
  • 3
  • 5
  • 14