@FunctionalInterface public interface 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:
To use virtual threads, provide an implementation that calls
Thread.ofVirtual().name(name).unstarted(task):
IThreadFactory virtualFactory = (task, name, daemon) ->
Thread.ofVirtual().name(name).unstarted(task);
The defaultFactory() method provides a standard implementation
that works with all Java versions:
IThreadFactory factory = IThreadFactory.defaultFactory();
For application servers that manage their own thread pools:
IThreadFactory serverFactory = (task, name, daemon) -> {
Thread t = myThreadPool.newThread(task);
t.setName(name);
t.setDaemon(daemon);
return t;
};
IP2LocationService service = new IP2LocationService.Builder()
.downloadToken("your-token")
.threadFactory((task, name, daemon) -> {
Thread t = new Thread(task, name);
t.setDaemon(daemon);
t.setPriority(Thread.MIN_PRIORITY); // Low priority for background work
return t;
})
.build();
IP2LocationService.Builder.threadFactory(IThreadFactory)| Modifier and Type | Method and Description |
|---|---|
static IThreadFactory |
defaultFactory()
Returns the default thread factory implementation.
|
static IThreadFactory |
defaultWithExceptionHandler(Thread.UncaughtExceptionHandler handler)
Creates the default Virtual Thread factory with exception handling.
|
Thread |
newThread(Runnable task,
String name,
boolean daemon)
Creates a new thread for the specified task.
|
static IThreadFactory |
withExceptionHandler(IThreadFactory delegate,
Thread.UncaughtExceptionHandler handler)
Creates a thread factory that wraps another factory with exception
handling.
|
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.
Implementations should respect the daemon parameter, as daemon
threads are used for background tasks that should not prevent JVM shutdown,
while non-daemon threads are used for critical operations that must
complete.
task - The runnable task to executename - The suggested name for the thread (e.g., "IP2Location-Updater")daemon - true if the thread should be a daemon thread,
false for a user threadstatic IThreadFactory 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;
}
co.mindus.ip2location.IVirtualThreadFactory will be used instead.static IThreadFactory 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.
handler - The exception handler to install.static IThreadFactory withExceptionHandler(IThreadFactory 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 factory.handler - The exception handler to install.Phantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.