https://kotlinlang.org logo
Title
w

WukongRework.exe

01/26/2021, 7:17 AM
So I was doing some tests with newSingleThreadContext and withContext and I was wondering, does withContext block the parent coroutineScope?
import kotlinx.coroutines.*
fun main() = runBlocking {
    launch {
        delay(100)
        println("@ launch")
    }
    withContext(newSingleThreadContext("new Context")) {
        delay(200)
        println("@ withContext")
    }
    println("@ runBlocking")
}
// Outputs
@ launch
@ withContext
@ runBlocking
but
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch {
        delay(100)
        println("@ launch")
    }
    launch(newSingleThreadContext("new Context")) {
        delay(200)
        println("@ withContext")
    }
    println("@ runBlocking")
}
// Outputs
@ runBlocking
@ launch
@ withContext
could someone please explain this?
t

Tijl

01/26/2021, 8:17 AM
yes, that’s the point of
withContext
as opposed to
launch
a

andylamax

01/26/2021, 2:39 PM
withContext
suspends the coroutine until all of its children completes