You can run a coroutine that is waiting for completion, and then close resultChannel .
First, rewrite the code that runs consumers to save Job s:
val jobs = (1..threads).map { launch(CommonPool) { bundleProducer.consumeEach { println("CONSUMING $it") resultChannel.send("Result ($it)") } } }
Then run another coroutine that closes the channel after all Job complete:
launch(CommonPool) { jobs.forEach { it.join() } resultChannel.close() }
source share