Coroutine
subroutines : 同時に実行している関数は1つで、return した場合にはスタックを消費する
coroutine : 同時に実行している関数は1つで、call した際にスタックを別で作りスタック間を行ったり (resume)、戻ったり (yield) することができるという構造
※並行で処理すること自体は subroutine と coroutine の違いではなくそれはただの並行プログラミングの話
https://gyazo.com/4bcd1807c18620af382fef4995ff6273 https://gyazo.com/1bb4d4b39da907a492999549b9c0cca8
Coroutines provide concurrency without parallelism: when one coroutine is running, the one that resumed it or yielded to it is not.
Threads provide more power than coroutines, but with more cost. The additional power is parallelism, and the cost is the overhead of scheduling, including more expensive context switches and the need to add preemption in some form. Typically the operating system provides threads, and a thread switch takes a few microseconds.
Generators provide less power than coroutines, because only the top-most frame in the coroutine is allowed to yield. That frame is moved back and forth between an object and the call stack to suspend and resume it.
余談として、 Kotlin の Sequence も Generator
参考