Class VirtualThreads

java.lang.Object
com.iizix.virtual.VirtualThreads

public class VirtualThreads extends Object
Support for better concurrency support in IIZI by means of VirtualThreads Threads in the JVM.

It is the early form of virtual threads for the IIZI system supported by Java 19 and better. Functionality to support it must be enabled as "--enabled-preview" as a parameter to the JVM.

A property "iizi.virtualthreads=false" will turn off the support regardless of JVM settings, e.g. using the Java parameter -Diizi.virtualthreads=false.

  • Constructor Details

    • VirtualThreads

      public VirtualThreads()
  • Method Details

    • areVirtualThreadsSupported

      public static boolean areVirtualThreadsSupported()
      Returns if the virtual threads are supported on the platform.
      Returns:
      true if supported, false otherwise.
    • startThread

      public static Thread startThread(Runnable runnable)
      Starts a new "real" thread or a virtual thread depending on JVM support. The thread is started as a daemon thread with the priority Thread.NORM_PRIORITY, but only when not a virtual thread.
      Parameters:
      runnnable - The runnable.
      Returns:
      The new started thread.
    • startThread

      public static Thread startThread(Runnable runnable, String name)
      Starts a new "real" thread or a virtual thread depending on JVM support. The thread is started as a daemon thread with the priority Thread.NORM_PRIORITY, but only when not a virtual thread.
      Parameters:
      name - The thread name (or null for none.
      runnnable - The runnable.
      Returns:
      The new started thread.
    • startThread

      public static Thread startThread(Runnable runnable, String name, int priority)
      Starts a new "real" thread or a virtual thread depending on JVM support. The thread is started as a daemon thread with the priority specified, but only when not a virtual thread.
      Parameters:
      name - The thread name (or null for none.
      priority - The thread priority (always ignored for virtual threads).
      runnnable - The runnable.
      Returns:
      The new started thread.
    • isVirtualThread

      public static boolean isVirtualThread(Thread thread)
      Checks if the thread is virtual.
      Parameters:
      thread - The thread to test.
      Returns:
      true if thread is virtual, false otherwise.
    • isVirtualCurrentThread

      public static boolean isVirtualCurrentThread()
      Checks if the current thread is virtual.
      Returns:
      true if thread is virtual, false otherwise.
    • newExecutorForProcessor

      public static ExecutorService newExecutorForProcessor()
      Creates a new executors service based on virtual threads or the number of CPU's available.

      Executors.newVirtualThreadPerTaskExecutor() creates an ExecutorService that creates a new virtual thread for each task.

      In the case if virtual threads is not supported, the return value will be a new new ForkJoinPool().

      Returns:
      The executor service.