Sagar Malhotra
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.

--

--

Sagar Malhotra
Sagar Malhotra

Written by Sagar Malhotra

Android dev by day, Creator by night.🥷🏻 Sharing my passion through videos, blogs and conferences.🚀

No responses yet