Stephan Schroeder
07/22/2019, 3:17 PMfun main() = runBlocking { // this: CoroutineScope
coroutineScope {
launch {
delay(200L)
println("2Task from runBlocking")
}
coroutineScope { // Creates a coroutine scope
launch {
delay(500L)
println("3Task from nested launch")
}
delay(100L)
println("1Task from coroutine scope") // This line will be printed before the nested launch
}
println("4Coroutine scope is over") // This line is not printed until the nested launch completes
}
}
withoutclass
07/22/2019, 3:19 PMrunBlocking
is waiting for it's children to complete. coroutineScope
is a child of your runBlocking
gildor
07/22/2019, 3:20 PMStephan Schroeder
07/22/2019, 3:21 PMrunBlocking
and coroutineScope
.
If there is a difference between the two (and I’m sure there is), why doesn’t the output change??withoutclass
07/22/2019, 3:23 PMcoroutineScope
inside a runBlocking
, which means that you're going to block while your runBlocking
scope waits for it's child coroutineScope
to complete.gildor
07/22/2019, 3:23 PMMarko Mitic
07/22/2019, 3:24 PMcoroutineScope
inherits `runBlocking`s context, so it executes everything in single threadgildor
07/22/2019, 3:25 PMStephan Schroeder
07/22/2019, 3:28 PMcoroutineScope
isn’t blocking the main thread, why isn’t println("4Coroutine scope is over")
executed immediately?gildor
07/22/2019, 3:31 PMStephan Schroeder
07/22/2019, 3:31 PMgildor
07/22/2019, 3:32 PMStephan Schroeder
07/22/2019, 3:32 PMgildor
07/22/2019, 3:32 PMStephan Schroeder
07/22/2019, 3:34 PMgildor
07/22/2019, 3:34 PMStephan Schroeder
07/22/2019, 3:35 PMsuspend
is definetly the new thing.gildor
07/22/2019, 3:35 PMStephan Schroeder
07/22/2019, 3:35 PMgildor
07/22/2019, 3:35 PMStephan Schroeder
07/22/2019, 3:35 PMgildor
07/22/2019, 3:36 PMStephan Schroeder
07/22/2019, 3:36 PMgildor
07/22/2019, 3:38 PMStephan Schroeder
07/22/2019, 3:39 PMgildor
07/22/2019, 3:39 PMStephan Schroeder
07/22/2019, 3:40 PMgildor
07/22/2019, 3:40 PMStephan Schroeder
07/22/2019, 3:40 PMgildor
07/22/2019, 3:43 PMStephan Schroeder
07/22/2019, 3:51 PMgildor
07/23/2019, 12:47 PMStructured Concurrency blog post didn’t helpYou said that “but there are no scopes in the async-await model”, so I just pointed you to description of this approach and why it’s here to avoid confsion
So basically runBlocking is a normal function while coroutineScope is a suspend function but both create a new CoroutineScopeThat’s correct
treats the lambda provided to the function in the same wayYeah, I would say not lambda, but they provide coroutine scope and scope works the same for all builders, not only for those two, but builders with other semantics and usecases (like withContext, launch etc), it provides scope where you can run child coroutines (even background ones) without worry about lifecycle and leaks of them
Stephan Schroeder
07/23/2019, 1:33 PMI would say not lambda,pretty sure it’s a lambda with (CoroutineScope) receiver. Hmm, so i checked the API for coroutineScope
suspend fun <R> coroutineScope(
block: suspend CoroutineScope.() -> R
): R (source)
without the suspend
, block would be a lambda with receiver. I’m not sure if the name changes because of the suspend
(not the first suspend, the second after block).
Maybe to “suspending lambda with receiver”?!?
But now we’re down to how to name things. I feels like the original issue has been resolved 🙂gildor
07/23/2019, 3:29 PM