CountDownLatch implementation
See original GitHub issueIn JDK we have CountDownLatch. It’d be useful to have a CountDownLatch
which is aware of coroutines. It could have the following interface:
interface CountDownLatch {
fun countDown()
fun getCount(): Long
suspend fun await()
suspend fun await(time: Long, unit: TimeUnit = TimeUnit.MILLISECONDS): Boolean
}
I created two different implementations:
- First which uses a list of
Continuation
s which are resumed when the counter goes down to 0, - Second which uses
ConflatedBroadcastChaneel
holding current state of latch. Note that thecountDown
function in this version is suspending.
I’m sure a better(faster!) implementation could be made.
Looking for an opinion on this.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:7 (5 by maintainers)
Top Results From Across the Web
Guide to CountDownLatch in Java - Baeldung
In this article, we'll give a guide to the CountDownLatch class and demonstrate how it can be used in a few practical examples....
Read more >CountDownLatch (Java Platform SE 7 ) - Oracle Help Center
A CountDownLatch is initialized with a given count. The await methods block until the current count reaches zero due to invocations of the...
Read more >What is the better way to implement CountDownLatch in ...
Effective Java Item 72 shows bad example of CountDownLatch implementation. But it doesn't show a right way to implement it.
Read more >Implementation of custom/own CountDownLatch in java
Implementation of custom/own CountDownLatch in java - Detailed explanation with full program · CountDownLatchCustom wait until one or more threads completes ...
Read more >CountDownLatch in Java - GeeksforGeeks
Working of CountDownLatch: When we create an object of CountDownLatch, we specify the number of threads it should wait for, all such thread...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Mahoney You can use a locked mutex (that you unlock to signal event), or a semaphore (that you release to signal an event), or a channel (that you offer an event to) for this particular snippet. Any of those primitives would work.
I’m curious as to how I should go about implementing
sendMessageAndWaitUntilProcessed
without a CountDownLatch (or equivalent) in this gist: https://gist.github.com/Mahoney/52fa3184f5af6b00a380cb778cc551ca