question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CountDownLatch implementation

See original GitHub issue

In 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 Continuations which are resumed when the counter goes down to 0,
  • Second which uses ConflatedBroadcastChaneel holding current state of latch. Note that the countDown 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
elizarovcommented, May 14, 2020

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

@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.

0reactions
Mahoneycommented, May 14, 2020

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found