Timur Atakishiev
01/28/2020, 10:04 AMsomeList.forEach {
GlobalScope.launch {
if (validator.validate(it)){
someStorage.add(it)
}
}
}
Hi guys, I am expecting that validate function is going to be excecuted in N numbers(N = size of someList) of coroutines. hewever, my application is working sequentialy, first it is validating first element, second element and so on. Should it work sequentially? Or am I doing something wrong?paulex
01/28/2020, 10:36 AMlaunch
for each item?Timur Atakishiev
01/28/2020, 10:39 AMpaulex
01/28/2020, 10:59 AMimport kotlinx.coroutines.delay
import kotlinx.coroutines.launch
suspend fun addAfterValidate(n:Int){
delay(3000L)
println("validating $n\n")
}
arrayListOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).forEach {
GlobalScope.launch {
addAfterValidate(it)
}
}
Timur Atakishiev
01/28/2020, 11:03 AMvalidate
function is making some long running process. In My case there are 5 parallel calls of validate
and then it is waits till one of that 5 parallel calls is going to be endpaulex
01/28/2020, 11:04 AMTimur Atakishiev
01/28/2020, 11:09 AMvalidate
is using RestTemplate, and makes some get request certain URI, is it blocking scope?coroutineScope {
someList.forEach {
launch(Executors.newSingleThreadExecutor().asCoroutineDispatcher()) {
if (validator.validate(it)) {
<http://log.info|log.info>("validator called on $it")
someStorage.add(it)
}
}
}
}
Found a solution for my problem, who knows whether it is okay or not?