Joining Jobs

With your producer and first worker in place, you are ready for an additional worker. Add a parameter to control the number of workers that you are allowed to use, then spin up additional worker coroutines. Also add two travelers to the list of passengers being tracked so there are more flights for your simulation.

Listing 22.7  Launching multiple workers (FlightWatcher.kt)

...
suspend fun fetchFlights(
    passengerNames: List<String> = listOf("Madrigal", "Polarcubis",
        "Estragon", "Taernyl"),
    numberOfWorkers: Int = 2 ): List<FlightStatus> = coroutineScope { val passengerNamesChannel = Channel<String>() val fetchedFlightsChannel = Channel<FlightStatus>() launch { passengerNames.forEach { passengerNamesChannel.send(it) } ...

Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.