Package com.iizix.virtual
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 SummaryFieldsModifier and TypeFieldDescription- static final AtomicLongThe static Atomic Long counter for unnamed threads.
- Method SummaryModifier and TypeMethodDescription- booleanChecks if Virtual Threads are supported, i.e.- createThreadFactory- (ThreadGroup threadGroup, Loggers loggers, String name) Creates a thread factory with the specified loggers instance and thread name.- booleanChecks if the current thread is virtual.- boolean- isVirtualThread- (Thread thread) Checks if the thread is virtual.Creates a new executors service based on virtual threads or the number of CPU's available.- default Thread- startThread- (Runnable runnable) Starts a new "real" thread or a virtual thread depending on JVM support.- default Thread- startThread- (Runnable runnable, String name) Starts a new "real" thread or a virtual thread depending on JVM support.- startThread- (Runnable runnable, String name, int priority) Starts a new "real" thread or a virtual thread depending on JVM support.
- Field Details- counterThe static Atomic Long counter for unnamed threads.
 
- Method Details- areSupportedboolean areSupported()Checks if Virtual Threads are supported, i.e. that JEP 444 is present, or Java 19 or Java 20 is running with- --enable-previewflag set.- Returns:
- trueif enabled,- falseotherwise.
 
- createThreadFactoryCreates 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.
 
- startThreadStarts 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.
 
- startThreadStarts 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.
 
- startThreadStarts a new "real" thread or a virtual thread depending on JVM support. The thread is started as a daemon thread with the- priorityspecified, 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- nameis- null.
 
- isVirtualThreadChecks if the thread is virtual.- Parameters:
- thread- The thread to test.
- Returns:
- trueif thread is virtual,- falseotherwise.
 
- isVirtualCurrentThreadboolean isVirtualCurrentThread()Checks if the current thread is virtual.- Returns:
- trueif thread is virtual,- falseotherwise.
 
- newExecutorForProcessorExecutorService newExecutorForProcessor()Creates a new executors service based on virtual threads or the number of CPU's available.- Executors.newVirtualThreadPerTaskExecutor([factory])creates an- ExecutorServicethat 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.