Does anyone know if there is any latch implementation that does the following:
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With