Java / Java 21 Virtual Threads Interview questions
What is the difference between virtual threads and Kotlin coroutines or Go goroutines?
All three are lightweight, cooperatively scheduled units of concurrency, far cheaper than OS threads, but they sit at different layers of their respective platforms.
Kotlin coroutines are a language and library-level feature: functions marked suspend are transformed by the compiler into continuation-passing code, and you must launch them inside an explicit coroutine builder or scope - calling a suspend function from ordinary code isn't allowed without that machinery.
Go goroutines are runtime-managed and woven tightly into the language, with channels as a first-class primitive under a single unified scheduler.
Java virtual threads are implemented at the JVM and class-library level as genuine java.lang.Thread instances. This means existing blocking Java code - plain I/O, JDBC calls, synchronized collections - runs unmodified, without the "function coloring" problem where async and sync code can't freely call each other, which is a real constraint with Kotlin's suspend keyword.
More Related questions...