3 ms·
The primary difference between Loom's virtual threads and Kotlin's virtual threads (coroutines) are: - Coroutines have a higher overhead. Though not huge, Lo
by gavinray 4y ago
The primary difference between Loom's virtual threads and Kotlin's virtual threads (coroutines) are:
- Coroutines have a higher overhead. Though not huge, Loom threads are nearly free.
- Kotlin Coroutines aren't pre-emptible (and this is the big one). Loom's threads are pre-emptible. This property is why they can be more efficient.
- mwcampbell 4y agoWhat I read in JEP 425 is that Loom threads aren't actually preempted by the JVM's user-space scheduler. They can be mapped onto an arbitrary number of OS threads, but if you use an OS thread per core, and one of your virtual threads is CPU-bound, it won't be preempted IIUC.
- jillesvangurp 4y agoFrom the spec "they are preempted when they block on I/O or synchronization.". That's nice but if you write some inefficient loop, your virtual thread won't be pre-empted. Also, Kotlin co-routines will obviously use Loom when it is available and offer the same benefits. All that takes is a few tweaks in the co-routines library. Mostly, that should not even be needed because Loom is API compatible with the old threadpool and thread APIs which already have extension functions to turn them into coroutine scopes. So, a lot of stuff should just benefit from Loom just by upgrading the JVM. Which is great of course.