Package com.iizix.virtual
Class VirtualThreads
java.lang.Object
com.iizix.virtual.VirtualThreads
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 Summary
Method Summary
Modifier and TypeMethodDescriptionstatic boolean
Returns if the virtual threads are supported on the platform.static boolean
Checks if the current thread is virtual.static boolean
isVirtualThread
(Thread thread) Checks if the thread is virtual.static ExecutorService
Creates a new executors service based on virtual threads or the number of CPU's available.static Thread
startThread
(Runnable runnable) Starts a new "real" thread or a virtual thread depending on JVM support.static Thread
startThread
(Runnable runnable, String name) Starts a new "real" thread or a virtual thread depending on JVM support.static Thread
startThread
(Runnable runnable, String name, int priority) Starts a new "real" thread or a virtual thread depending on JVM support.
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
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.- Parameters:
runnnable
- 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.- Parameters:
name
- The thread name (ornull for none
.runnnable
- 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 thepriority
specified, but only when not a virtual thread.- Parameters:
name
- The thread name (ornull for none
.priority
- The thread priority (always ignored for virtual threads).runnnable
- The runnable.- Returns:
- The new started thread.
isVirtualThread
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
Creates a new executors service based on virtual threads or the number of CPU's available.Executors.newVirtualThreadPerTaskExecutor()
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()
.- Returns:
- The executor service.