@FunctionalInterface public interface IVirtualThreadFactory extends IThreadFactory
This interface allows applications to control how threads are created for background tasks such as database downloads, scheduled updates, and asynchronous initialization. This is particularly useful for:
IP2LocationService service = new IP2LocationService.Builder()
.downloadToken("your-token")
.threadFactory(new VirtualThreadFactory(task, name, daemon))
.build();
IP2LocationService.Builder.threadFactory(IThreadFactory)| Modifier and Type | Method and Description |
|---|---|
static IVirtualThreadFactory |
defaultFactory()
Returns the default thread factory implementation.
|
static IVirtualThreadFactory |
defaultWithExceptionHandler(Thread.UncaughtExceptionHandler handler)
Creates the default Virtual Thread factory with exception handling.
|
default Thread |
newThread(Runnable task,
String name)
Creates a new Virtual thread for the specified task.
|
Thread |
newThread(Runnable task,
String name,
boolean daemon)
Creates a new Virtual thread for the specified task.
|
static IVirtualThreadFactory |
withExceptionHandler(IVirtualThreadFactory delegate,
Thread.UncaughtExceptionHandler handler)
Creates a thread factory that wraps another factory with exception
handling.
|
withExceptionHandlerdefault Thread newThread(Runnable task, String name)
The returned thread should be in the "new" state (not yet started). The caller is responsible for starting the thread.
The implementations does not respect the daemon-mode as it doesn't exist for Virtual Threads: all Virtual Threads are daemon threads.
Calling this executes the following code, effectively creating a new daemon Virtual Tread.
return newThread(task,name,true);
task - The runnable task to executename - The suggested name for the thread (e.g., "IP2Location-Updater")Thread newThread(Runnable task, String name, boolean daemon)
The returned thread should be in the "new" state (not yet started). The caller is responsible for starting the thread.
The Virtual Thread Implementations cannot respect the daemon parameter,
as all Virtual Threads are daemon threads, thus threads are used for background tasks
that should not prevent JVM shutdown.
Do not use non-daemon threads if they are used for critical operations that must complete.
newThread in interface IThreadFactorytask - The runnable task to executename - The suggested name for the thread (e.g., "IP2Location-Updater")daemon - This daemon variable is not used with Virtual Threads
as only daemon thread states exist in the Virtual Threads world.
NOTE: this parameter will be ignored, like if it was set to true.
static IVirtualThreadFactory defaultFactory()
This factory creates standard platform threads with the specified name and daemon status. It is suitable for all Java versions (8+).
The implementation is equivalent to:
(task,name,daemon) -> {
Thread t=new Thread(task,name);
t.setDaemon(daemon);
return t;
}
defaultFactory in interface IThreadFactorystatic IVirtualThreadFactory defaultWithExceptionHandler(Thread.UncaughtExceptionHandler handler)
The returned factory installs an uncaught exception handler on each created thread, ensuring that exceptions in background tasks are logged rather than silently swallowed.
defaultWithExceptionHandler in interface IThreadFactoryhandler - The exception handler to install.static IVirtualThreadFactory withExceptionHandler(IVirtualThreadFactory delegate, Thread.UncaughtExceptionHandler handler)
The returned factory installs an uncaught exception handler on each created thread, ensuring that exceptions in background tasks are logged rather than silently swallowed.
delegate - The underlying thread factoryhandler - The exception handler to installPhantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.