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
Modifier and TypeFieldDescriptionstatic final AtomicLong
The static Atomic Long counter for unnamed threads.Method Summary
Modifier and TypeMethodDescriptionboolean
Checks 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.boolean
Checks 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
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
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 thepriority
specified, 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
- Ifname
isnull
.
isVirtualThread
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 anExecutorService
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.