Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latch that can be incremented

Does anyone know if there is any latch implementation that does the following:

  • has a method to decrement the latch value, or wait if the value is zero
  • has a method for waiting for the latch value to be zero
  • has a method for adding a number to the latch's value
like image 667
Razvi Avatar asked Jan 10 '13 09:01

Razvi


People also ask

What is latch multithreading?

The CountDownLatch class is another important class for concurrent execution. It is a synchronization aid that allows one or more than one thread to wait until a set of operations being performed in another thread is completed.

What is the use of countDown latch?

CountDownLatch is used to make sure that a task waits for other threads before it starts. To understand its application, let us consider a server where the main task can only start when all the required services have started.

What is Java Phaser concurrency?

The Phaser is a barrier on which the dynamic number of threads need to wait before continuing execution. In the CountDownLatch that number cannot be configured dynamically and needs to be supplied when we're creating the instance.

How do I reset my CountDownLatch?

Every time when CountDownLatch. countDown() is called, the value of count will decrease by 1 until the value becomes 0. One thing to be noted is that the value of count cannot be reset. i.e, when count becomes 0, the CountDownLatch will not work anymore, the call to await() method will return immediately.


1 Answers

You could also use a Phaser (java.util.concurrent.Phaser)

final Phaser phaser = new Phaser(1); // register self while (/* some condition */) {     phaser.register(); // Equivalent to countUp     // do some work asynchronously, invoking     // phaser.arriveAndDeregister() (equiv to countDown) in a finally block } phaser.arriveAndAwaitAdvance(); // await any async tasks to complete 

I hope this helps.

like image 57
Michael-7 Avatar answered Sep 28 '22 03:09

Michael-7