Class VirtualThreadsPreviewExamples
java.lang.Object
net.jrodolfo.java_evolution.java19.VirtualThreadsPreviewExamples
Demonstrates virtual threads as a Java 19 preview feature.
Before virtual threads, scalable blocking code often forced a tradeoff: platform threads were easy to understand but expensive at very high counts, while asynchronous callback or reactive styles scaled better but were harder to read and debug. Virtual threads were introduced to make thread-per-task code scale much further while keeping the programming model familiar.
Virtual threads became final later, in Java 21. This project compiles on JDK 25, so the example uses the final API while documenting the Java 19 preview origin.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionStarts a virtual thread and returns its result.booleanCreates a virtual thread without starting it immediately.
-
Constructor Details
-
VirtualThreadsPreviewExamples
public VirtualThreadsPreviewExamples()
-
-
Method Details
-
runInVirtualThread
Starts a virtual thread and returns its result.- Returns:
- text produced inside a virtual thread
- Throws:
InterruptedException- when waiting is interruptedExecutionException- when the task fails
-
unstartedThreadIsVirtual
public boolean unstartedThreadIsVirtual()Creates a virtual thread without starting it immediately.- Returns:
- whether the created thread is virtual
-