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
2 answers

Usage of countDown latch in java

Am new to java programming and first time using countDown in java, My code snippet is, CountDownLatch latch=new CountDownLatch(rows*columns); //rows -2 , columns -3 for (int i = 0; i < rows; i++) { for (int j = 0; j <…
user3164187
  • 1,382
  • 3
  • 19
  • 50
1
vote
3 answers

how to know the exact time thread requires to finish

I have two threads t1 and t2. Both of them make some calculations and i am tryin to block the main thread till t1 and t2 finish. I used .awaitTermination() as seen below, but the problem is, despit it is an if-statement, the .awaitTermination() goes…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
1
vote
1 answer

How to make threads work in order using CountDownlatch?

I am learning how to use countdownLatch in java, and i created a simple example as shown below inthe code. what i learnt about that mechanism is, it is just a way to force only ONE thread to wait for others till they finish their work, then that…
rmaik
  • 1,076
  • 3
  • 15
  • 48
1
vote
2 answers

Using cyclic barrier does not wait till all threads finish

Here is what I am trying to do. I have a number of threads which should all wait at a common point before they proceed, so obvious solution is to use CyclicBarrier. But I want to also compute the total time taken by the threads to execute. I defined…
akhil_mittal
  • 23,309
  • 7
  • 96
  • 95
1
vote
6 answers

How does CountDownLatch works in Java?

I am studying Synchronization in Java. I am not able to understand the exact mechanism of CountDownLatch. Does CountDownLatch 'counts down the latch' (waits for completion of number of threads) as per the number of threads which are given at…
Anurag
  • 723
  • 3
  • 13
  • 31
1
vote
3 answers

Sending shut down signal to another thread in Java

I have an application that runs something repeatedly in a while loop in a separate thread until the user triggers a shut down signal. I can send the signal from main thread to the worker thread by setting a (volatile) boolean field in the worker…
senseiwu
  • 5,001
  • 5
  • 26
  • 47
1
vote
1 answer

Can I control order of thread execution with CountDownLatch?

I have task to do. I have to create 4 services A,B,C and D. Each service should have his own thread. A service should only start after all the services that it depends on are started and A service should only stop after all the services that…
njamanjam
  • 245
  • 2
  • 3
  • 9
1
vote
1 answer

piped streams and count down latch

I am trying to prototype a program that would read a stream then spin up new processing streams in a thread pool as the main thread got further along in the stream. I'm running into a problem with PipedStreams and CountDownLatch. When I run the…
darckeen
  • 960
  • 2
  • 12
  • 20
1
vote
1 answer

How can i create a remote latch in java?

At the moment I have a server that: 1) Launches 100 servlet requests using an executor on localhost 2) Executes the servlet requests. 3) Decrements a latch counting the total amount of completed servlets. 4) Calculates the execution time. I would…
Bedo
  • 925
  • 2
  • 14
  • 27
1
vote
0 answers

How would you implement joining of data from database and web-service in EJB3?

I need to fetch some data from external service. I dont know yet whether that service will be a simple servlet, or may be web-service, or something else. Lets assume that by now external service is just a simple servlet which returns JSON after HTTP…
KutaBeach
  • 1,445
  • 21
  • 43
1
vote
2 answers

Java - Count in COUNTDOWNLATCH

While running each thread why does the countdown.getCount() always print '3' even after the previous thread has already called countdown.countDown() and reduced the Latch Count by 1? I am kind of worndering how Java knows that the Latch Count has…
user547453
  • 1,035
  • 6
  • 22
  • 38
1
vote
1 answer

Multiple threads doesn't work

I am trying to run a code with a GUI interface. But I need the GUI windows all to be closed before I proceed with my main method, (some of the information that I need is collected from the GUI windows and I cant run the rest of my code without that…
Mehdi
  • 21
  • 1
  • 1
  • 4
1
vote
1 answer

Synchronize Is Blocking When I Try To CountDown

I followed the advice I found in this post using CountDownLatch and i'm running into a problem. I wrote up this test and ran it and my thread I created blocks when i try to synchronize on lock. private CountDownLatch lock = new…
Ring
  • 2,249
  • 5
  • 27
  • 40
1
vote
1 answer

CountDownLatch issue in android

I am using countdownlatch as in example below. Here BackgroundDataPopulator creates an AyncTask which in turn invokes displayData in PackagePopulator. But it never happens and the activity gets stuck. What am I doing wrong here. public class…
sab
  • 9,767
  • 13
  • 44
  • 51
1
vote
2 answers

Java: ExecutorService thread synchronization with CountDownLatch causes dead lock?

I have written a game of life for programming practice. There are 3 different implementations of the generator. First: One main thread + N sub threads, Second: SwingWorker + N sub threads, Third: SwingWorker + ExecutorService. N is the number of…
Dima82
  • 11
  • 2