SrSouza
05/01/2020, 9:50 PMnewSingleThreadExecutor
as a Dispatcher but I have a problem with it, when I use delay(x)
, other functions that use the same dispatcher does not execute until the delay finish. Why this is happening if delay
does not block the Thread.octylFractal
05/01/2020, 9:56 PMoctylFractal
05/01/2020, 9:56 PMfun main() {
val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher()
runBlocking(dispatcher) {
launch {
println("Foo!")
delay(1000L)
println("After Foo!")
}
launch {
println("Bar!")
delay(1000L)
println("After Bar!")
}
}
}
produced:
Foo!
Bar!
After Foo!
After Bar!
SrSouza
05/01/2020, 9:59 PMwithContext(singleThread) {
coroutineScope.launch(singleThread) {
delay(x)
}
}
When I migrate it to a function thar produce a Job, fix the problem, But why ?elizarov
05/01/2020, 10:01 PMwithContext
suspends until all the coroutines launched in its scope complete.SrSouza
05/01/2020, 10:27 PMEyeCon
05/02/2020, 5:27 PMcoroutineScope {}
also does that, right? So it is only async
and launch
that fire off jobs that are executed in parallel, while a scope ending of coroutineScope
or withContext
suspends until every child coroutine is completed.octylFractal
05/02/2020, 5:29 PMasync
and launch
start parallel jobs, other things like produce
, actor
, and Flow.launchIn
can also do so -- many methods that take a coroutine scope and aren't suspend will start a separate jobEyeCon
05/02/2020, 8:57 PM