Class ThreadComparisonDemo

java.lang.Object
modernfeatures.java21.ThreadComparisonDemo

public class ThreadComparisonDemo extends Object
Demonstrates and compares classic (platform) threads and virtual threads in Java 21+.

This class launches several classic threads and several virtual threads, showing their creation, execution, and thread identity. It prints each thread's details and sleeps them briefly to illustrate their independent execution. The example highlights differences between classic and virtual threads, particularly in how they are created, managed, and reported by the JVM.

Note: This example uses Thread.sleep in the main method as a simple way to let child threads finish before the main thread exits. In production code, more robust synchronization (like join) should be used.

  • Constructor Details

    • ThreadComparisonDemo

      public ThreadComparisonDemo()
  • Method Details

    • main

      public static void main(String[] args) throws InterruptedException
      Entry point. Launches classic (platform) threads and virtual threads, each printing their identity and sleeping to simulate work. Thread.sleep is used in each thread to simulate workload and in the main method to give threads time to run before the main thread exits.
      Parameters:
      args - command line arguments (not used)
      Throws:
      InterruptedException - if the main thread is interrupted while sleeping