Aug 25, 2024
GlobalScope.launch {
print("Hello coroutine from 1 ${Thread.currentThread().name}")
delay(100L)
print("Hello coroutine from 2 ${Thread.currentThread().name}")
}
As you can check the output of this, coroutines are not always in a single thread, once suspended, they can easily switch between threads without the overhead of context switching. When CPU have to switch context in Threads, it had to work with Kernal level data structure and switch memory pages, which is costlier, which is not in the case of coroutines.