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 Summary
FieldsModifier and TypeFieldDescriptionstatic final AtomicLongThe static Atomic Long counter for unnamed threads.Method Summary
Modifier and TypeMethodDescriptionbooleanChecks 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.booleanisVirtualThread(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 ThreadstartThread(Runnable runnable) Starts a new "real" thread or a virtual thread depending on JVM support.default ThreadstartThread(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
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-previewflag set.- Returns:
trueif enabled,falseotherwise.
createThreadFactory
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
Starts a new "real" thread or a virtual thread depending on JVM support. The thread is started as a daemon thread with the priorityThread.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
Starts a new "real" thread or a virtual thread depending on JVM support. The thread is started as a daemon thread with the priorityThread.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 (ornull for none.- Returns:
- The new started thread.
startThread
Starts a new "real" thread or a virtual thread depending on JVM support. The thread is started as a daemon thread with thepriorityspecified, but only when not a virtual thread.- Parameters:
runnable- The runnable.name- The thread name, notnull.priority- The thread priority (always ignored for virtual threads).- Returns:
- The new started thread.
- Throws:
NullPointerException- Ifnameisnull.
isVirtualThread
Checks if the thread is virtual.- Parameters:
thread- The thread to test.- Returns:
trueif thread is virtual,falseotherwise.
isVirtualCurrentThread
boolean isVirtualCurrentThread()Checks if the current thread is virtual.- Returns:
trueif thread is virtual,falseotherwise.
newExecutorForProcessor
ExecutorService newExecutorForProcessor()Creates a new executors service based on virtual threads or the number of CPU's available.Executors.newVirtualThreadPerTaskExecutor([factory])creates anExecutorServicethat 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.