Re:Why you can have millions of goroutines but only thousand...
by gpderetta on Tuesday October 09, 2018 @03:49AM
(#18174043)
The 1:1 mapping is just an implementation detail of of current JVMs. Originally green threads (with an M:N scheduler) were used instead. IIRC they weren't even guaranteed to be preemptively scheduled, I don't know if this has changed since.
Technically os threads are also simply continuations.
You can implement Go's thread model (more generally known as green threads, coroutines, fibers, lightweight threads) on the JVM. This is precisely what Project Loom intends to do and what Scala effect systems already do. You construct a program in a type called IO that only describes a computation that can be suspended and resumed, unlike Java Runnables. These IO values are cooperatively scheduled to run on a fixed thread pool. Because these IO values just represent continuations, you can have millions of them cooperatively executing at the same time. And there is no context switch penalty for switching the continuation you are executing for another.