Java / Java 21 Virtual Threads Interview questions
Explain how virtual threads interact with native code and JNI calls?
When a virtual thread enters native code through JNI, or performs an operation the JVM hasn't made "Loom-aware," it pins to its current carrier thread for the entire duration of that native call.
This happens because the JVM has no general mechanism to suspend a native call mid-execution and resume it later on a different OS thread the way it can with its own bytecode-level continuations - once you're inside native code, the underlying OS thread genuinely has to sit there and wait.
Heavy or long-running native calls can therefore quietly erode virtual threads' scalability advantage, since each one occupies a full carrier thread that could otherwise be serving other work. A common mitigation is isolating native-heavy code onto a small, dedicated pool of platform threads instead of invoking it directly from virtual threads, keeping the effect contained rather than let loose across your carrier pool.
More Related questions...