October 2021
Intermediate to advanced
500 pages
16h 23m
English
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) } ...Read now
Unlock full access