A flexible and efficient custom thread pool implementation in Java that provides variable thread management with configurable minimum and maximum thread counts, idle timeouts, and different task polling strategies.
- Variable Thread Pool Size: Configure minimum and maximum number of threads
- Idle Thread Management: Automatic thread termination after configurable idle time
- Core vs Non-Core Workers: Distinction between persistent core threads and scalable non-core threads
- Custom Thread Factory Support: Use your own thread factory or default to virtual threads
- Multiple Polling Strategies: Different task polling behaviors for various thread pool states
- Task Completion Tracking: Monitor completed task counts across all workers
- Standard ExecutorService Interface: Implements
AbstractExecutorServicefor compatibility
import adrian.os.java.threadpool.CustomThreadPool;
// Create a thread pool with default settings
CustomThreadPool threadPool = CustomThreadPool.builder().build();
// Submit a task
threadPool.submit(() -> {
System.out.println("Task executed by: " + Thread.currentThread().getName());
});
// Shutdown when done
threadPool.shutdown();import java.time.Duration;
import java.util.concurrent.ThreadFactory;
CustomThreadPool threadPool = CustomThreadPool.builder()
.setName("MyWorker") // Name prefix for worker threads
.setMinThreads(2) // Minimum 2 core threads
.setMaxThreads(10) // Maximum 10 threads
.setIdleTime(Duration.ofSeconds(30)) // Idle timeout of 30 seconds
.setThreadFactory(Thread.ofPlatform().factory()) // Use platform threads
.build();
// Submit multiple tasks
for (int i = 0; i < 100; i++) {
final int taskId = i;
threadPool.submit(() -> {
System.out.println("Processing task " + taskId);
// Your task logic here
});
}
// Monitor progress
System.out.println("Completed tasks: " + threadPool.getCompletedTasksCount());
System.out.println("Active workers: " + threadPool.getWorkers().size());
threadPool.shutdown();| Parameter | Default Value | Description |
|---|---|---|
name |
JVM default | Name prefix for worker threads |
minThreads |
0 |
Minimum number of core threads that persist even when idle |
maxThreads |
Integer.MAX_VALUE |
Maximum number of threads that can be created |
idleTime |
10 seconds |
Time after which idle non-core threads are terminated |
threadFactory |
Thread.ofVirtual().factory() |
Factory for creating new threads |
CustomThreadPool: Main thread pool implementation extendingAbstractExecutorServiceWorker: Individual worker threads that execute tasksITaskPollingStrategy: Interface defining task polling behavior- Polling Strategies:
RunningTaskPollingStrategy: For active thread poolsShutdownTaskPollingStrategy: For graceful shutdownTerminatedTaskPollingStrategy: For terminated pools
The thread pool distinguishes between two types of workers:
- Core Workers: Created up to
minThreadscount, persist even when idle - Non-Core Workers: Created on-demand up to
maxThreads, terminated after idle timeout
- NOT_RUNNING: Initial state and state after termination
- RUNNING: Active state processing tasks
- SHUTDOWN: Graceful shutdown in progress
The thread pool automatically starts upon construction and can be manually controlled using start(), shutdown(), and shutdownNow() methods.
CustomThreadPool pool = CustomThreadPool.builder()
.setMinThreads(Runtime.getRuntime().availableProcessors())
.setMaxThreads(Runtime.getRuntime().availableProcessors())
.setThreadFactory(Thread.ofPlatform().factory())
.build();
// Submit CPU-bound tasks
for (int i = 0; i < 1000; i++) {
pool.submit(() -> {
// simulate CPU-intensive computation
double result = Math.sqrt(Math.random() * 1000000);
});
}CustomThreadPool pool = CustomThreadPool.builder()
.setMinThreads(0)
.setMaxThreads(1000)
.setIdleTime(Duration.ofSeconds(5))
.setThreadFactory(Thread.ofVirtual().factory()) // Default
.build();
for (int i = 0; i < 10000; i++) {
pool.submit(() -> {
try {
// Simulate I/O operation
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
}See CustomThreadPoolTest.java for more examples.
- Java 24+
- JUnit 5
This project is licensed under the MIT License - see the LICENSE file for details.
Adrian-26-Isotope