Package dev.langchain4j.internal
Class VirtualThreadUtils
java.lang.Object
dev.langchain4j.internal.VirtualThreadUtils
Utility class for working with virtual threads introduced in Java 21.
This class provides a mechanism to create a virtual thread per task executor
through reflection, enabling compatibility with both Java versions that
support virtual threads and those that do not.
- Author:
- Konstantin Pavlov
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable ExecutorService
createVirtualThreadExecutor
(Supplier<@Nullable ExecutorService> fallback) Creates a virtual thread per task executor using reflection.
-
Constructor Details
-
VirtualThreadUtils
public VirtualThreadUtils()
-
-
Method Details
-
createVirtualThreadExecutor
public static @Nullable ExecutorService createVirtualThreadExecutor(Supplier<@Nullable ExecutorService> fallback) Creates a virtual thread per task executor using reflection. This allows code to run on both Java 21+ (where virtual threads are available) and earlier Java versions.- Parameters:
fallback
- aSupplier
that provides a fallbackExecutorService
when virtual threads are not available.- Returns:
- an
ExecutorService
using virtual threads if supported, otherwise a fallback implementation.
-