i'm learning about kotlin coroutines and scopes and can't understand how to trigger parallel calls by not blocking the main thread. As you can see in the following example 2 calls are triggered in parallel, however the call to .awaitAll() seems to be blocking the main thread as i would expect "I'm blocked message" to be printed before "1.Current thread ..." message. Is this the correct way to implement this type of queries?
current output:
Main thread main
...(After 5 sec) ...
1.Current thread DefaultDispatcher-worker-1
1.Current thread DefaultDispatcher-worker-3
I'm blocked
expected Output:
Main thread main
I'm blocked
...(After 5 sec) ...
1.Current thread DefaultDispatcher-worker-1
1.Current thread DefaultDispatcher-worker-3
class FindItinerariesComposable(private val providers: List<FindItineraries>) : FindItineraries {
override suspend fun itinerariesForOriginDestination(origin: String, destination: String): List<Itinerary> {
println("Main thread ${Thread.currentThread().name}")
return coroutineScope {
val stream = providers.stream()
.map {
async {
it.itinerariesForOriginDestination(origin, destination)
}
}
.toList()
.awaitAll()
.flatten()
println("I'm blocked")
stream
}
}
}
class FindItinerariesInFlightProvider(private val httpClient: HttpClient) : FindItineraries {
override suspend fun itinerariesForOriginDestination(origin: String, destination: String): List<Itinerary> {
withContext(Dispatchers.IO) {
delay(5000)
println("1.Current thread ${Thread.currentThread().name}")
}
return listOf(Itinerary("bue", "MIA", "TEST"))
}
}
fun main() = runBlocking<Unit> {
val service = FindItinerariesComposable(listOf(FindItinerariesInFlightProvider(HttpClient()),
FindItinerariesInFlightProvider(HttpClient())))
launch {
service.itinerariesForOriginDestination("BUE", "MIA")
}
}