Interface IVirtualThreads


public interface IVirtualThreads
The interface used to handle virtual threads in Java 21 (JEP 444), or when the Java option --enable-preview is set on the command line for Java 19 or 20.
Author:
Christopher Mindus
  • Field Details

    • counter

      static final AtomicLong counter
      The static Atomic Long counter for unnamed threads.
  • Method Details

    • areSupported

      boolean areSupported()
      Checks if Virtual Threads are supported, i.e. that JEP 444 is present, or Java 19 or Java 20 is running with --enable-preview flag set.
      Returns:
      true if enabled, false otherwise.
    • createThreadFactory

      ThreadFactory createThreadFactory(ThreadGroup threadGroup, Loggers loggers, String name)
      Creates a thread factory with the specified loggers instance and thread name.
      Parameters:
      threadGroup - The thread group is only used for platform threads.
      loggers - The loggers instance.
      name - The name of the thread factory for thread naming.
      Returns:
      The Virtual ThreadFactory instance.
    • startThread

      default 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.

      Calling this method is the same as calling startThread(runnable,null,Thread.NORM_PRIORITY).

      Parameters:
      runnable - The runnable.
      Returns:
      The new started thread.
    • startThread

      default 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.

      Calling this method is the same as calling startThread(runnable,name,Thread.NORM_PRIORITY).

      Parameters:
      runnable - The runnable.
      name - The thread name (or null for none.
      Returns:
      The new started thread.
    • startThread

      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:
      runnable - The runnable.
      name - The thread name, not null.
      priority - The thread priority (always ignored for virtual threads).
      Returns:
      The new started thread.
      Throws:
      NullPointerException - If name is null.
    • isVirtualThread

      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

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

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

      Executors.newVirtualThreadPerTaskExecutor([factory]) 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() and the number of CPU's times four.

      Returns:
      The executor service.