Class VirtualThreadsPreviewExamples

java.lang.Object
net.jrodolfo.java_evolution.java19.VirtualThreadsPreviewExamples

public class VirtualThreadsPreviewExamples extends Object
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 Details

    • VirtualThreadsPreviewExamples

      public VirtualThreadsPreviewExamples()
  • Method Details

    • runInVirtualThread

      public String runInVirtualThread() throws InterruptedException, ExecutionException
      Starts a virtual thread and returns its result.
      Returns:
      text produced inside a virtual thread
      Throws:
      InterruptedException - when waiting is interrupted
      ExecutionException - when the task fails
    • unstartedThreadIsVirtual

      public boolean unstartedThreadIsVirtual()
      Creates a virtual thread without starting it immediately.
      Returns:
      whether the created thread is virtual