Virtual thread API migration failures from JDK 19/20 preview to JDK 21 final
0 reputation · 27 Feb 2023, 03:23 UTC
0 reputation · 27 Feb 2023, 03:23 UTC
Goal: diagnose why code that compiled and ran with the preview virtual thread API in JDK 19 or JDK 20 fails when compiled or executed on JDK 21, where virtual threads are finalized and the API has changed.
Constraints: the code may use preview‑only imports (jdk.incubator.concurrent), preview‑only constructors, and custom scheduler implementations that relied on preview‑only classes; there is uncertainty about which preview methods were removed, what their replacements are, and how refined thread‑pinning behavior in the final release affects libraries that synchronize or call native code.
End with specific questions:
The virtual thread API has been finalized in JDK 21 with the following key changes:
jdk.incubator.concurrent to standard packages java.lang and java.util.concurrent.java.lang.Thread that no longer requires preview flags.FiberScheduler interface was removed entirely.import jdk.incubator.concurrent.Something with appropriate standard imports.--enable-preview on JDK 21 for virtual thread features.Executors.newVirtualThreadPerTaskExecutor() for custom scheduling needs.synchronized blocks with ReentrantLock when calling native code or performing blocking I/O.JDK 21 enforces stricter thread pinning behavior. Virtual threads holding monitors (via synchronized) or executing native methods will block their carrier thread. To avoid performance issues:
java.util.concurrent.locks.ReentrantLock instead of synchronized for critical sections.-Djdk.trace.threads to identify pinning hotspots during testing.If issues persist after migration, please provide:
synchronized blocks in I/O paths.Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.