Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Switch the context in the middle of the couroutine execution by using withContext() function
import kotlinx.coroutines.*
import java.util.concurrent.Executors
suspend fun suspendTask1() {
println("Start task1 | Thread ${Thread.currentThread()}")
println("End task1 | Thread ${Thread.currentThread()}")
suspend fun suspendTask2() {
println("Start task2 | Thread ${Thread.currentThread()}")
println("End task2 | Thread ${Thread.currentThread()}")
fun main() {
runBlocking {
println("Start main")
println("Starting in Thread ${Thread.currentThread()}")
withContext(Dispatchers.Default) { suspendTask1() }
launch { suspendTask2() }
println("Ending in Thread ${Thread.currentThread()}")
println("End main")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment