https://kotlinlang.org logo
#coroutines
Title
# coroutines
w

winteryoung

08/03/2018, 2:09 AM
This hasn't got an effective answer. I'm getting the same problem. Anyone can help?
e

elizarov

08/03/2018, 8:43 AM
Can you elaborate a bit on your specific use-case, please. What exactly you are trying to achieve by waiting until the actor stops?
w

winteryoung

08/03/2018, 4:38 PM
Copy code
private fun <T> asyncProduceConsume(
            consumerConcurrencyLimit: Int,
            producer: Producer<T>,
            consumer: Consumer<T>
    ) {
        runBlocking {
            actor<T> {
                arrayListOf<Job>().let { jobs ->
                    CoroutineSemaphore(consumerConcurrencyLimit).let { semaphore ->
                        for (product in this) {
                            launch {
                                semaphore.lock {
                                    consumer(product)
                                }
                            }.let { job ->
                                jobs.add(job)
                            }
                        }
                    }

                    for (job in jobs) {
                        job.join()
                    }
                }
            }.let { consumerActor ->
                async {
                    producer(consumerActor)
                }.also {
                    try {
                        it.await()
                    } finally {
                        consumerActor.close()
                        (consumerActor as Job).join()
                    }
                }
            }
        }
    }
I was trying to return the result only when the parent consumer actor finished consuming the products.