Is there way to get the same behavior as code snippet below has but using coroutines?
Updated code snippet:
fun main(args: Array<String>) = runBlocking {
val executor = Executors.newFixedThreadPool(50)
log.info("Start")
val jobs = List(300) {
executor.submit {
log.info("worker #$it started")
sleep(1000L)
log.info("worker #$it done")
}
}
jobs.forEach { it.get() }
executor.shutdown()
log.info("All done!")
}
How can I run 300 jobs with parallelism factor == 50, but without creation 50 real threads?
Update 2: Solution
After reading Coroutines Guide one more time, I've found that Fan-out example is exactly what I was looking for. Thus, my example will look as follows:
fun produceTasks() = produce {
for (taskId in 1..300) {
send(
async(start = CoroutineStart.LAZY) {
delay(1000) // simulate long work
taskId
}
)
}
close()
}
fun launchWorker(index: Int, channel: ProducerJob<Deferred<Int>>) = launch {
channel.consumeEach {
val result = it.await()
log.info("Worker #$index done task #$result")
}
}
fun main(args: Array<String>) = runBlocking {
val tasks = produceTasks()
val workers = List(50) { launchWorker(it + 1, tasks) }
workers.forEach { it.join() }
log.info("Done")
}