Pritam Kadam
Nov 4 · 1 min read

JVM threads are directly mapped to native os threads and each thread has its own stack and takes at least 1 MB of memory. So this itself is a huge cost if you do the calculation in my introduction example where I demonstrate 100,000 threads vs 100,000 coroutines, theoretically, 100,000 threads can take 100,000 * 1 MB of memory which practically one can not afford. On the other hand, you can treat coroutine = instance of class just like other classes in your application.

An operating system is responsible for scheduling threads on the CPU and OS can decide when to take out thread from CPU and schedule another thread (This usually happens based on timeslicing — Preemptive Scheduling)

You can refer to this StackOverflow question to know more details.

    Pritam Kadam

    Written by

    Developer @Thoughtworks