CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-eclipse-jetty--jetty-util

Comprehensive Java utility library providing infrastructure and helper classes for the Eclipse Jetty web server

Pending
Overview
Eval results
Files

threading.mddocs/

Threading

Advanced threading utilities including thread pools, schedulers, execution strategies, and concurrency primitives optimized for server applications and high-performance networking.

Capabilities

ThreadPool Interface

Core thread pool abstraction extending Executor with server-specific features.

/**
 * Thread pool interface for server applications
 */
public interface ThreadPool extends Executor, LifeCycle {
    /** Wait for thread pool to terminate */
    void join() throws InterruptedException;
    
    /** Get total number of threads */
    int getThreads();
    
    /** Get number of idle threads */
    int getIdleThreads();
    
    /** Check if thread pool is low on threads */
    boolean isLowOnThreads();
    
    /** Sized thread pool interface */
    interface SizedThreadPool extends ThreadPool {
        int getMinThreads();
        int getMaxThreads();
        void setMinThreads(int threads);
        void setMaxThreads(int threads);
    }
}

QueuedThreadPool

Main thread pool implementation with configurable sizing and queuing.

/**
 * Queued thread pool implementation
 */
public class QueuedThreadPool extends ContainerLifeCycle implements ThreadPool.SizedThreadPool {
    /** Create thread pool with default settings */
    public QueuedThreadPool();
    
    /** Create thread pool with max threads */
    public QueuedThreadPool(int maxThreads);
    
    /** Create thread pool with min/max threads */
    public QueuedThreadPool(int maxThreads, int minThreads);
    
    /** Set minimum number of threads */
    public void setMinThreads(int minThreads);
    
    /** Set maximum number of threads */
    public void setMaxThreads(int maxThreads);
    
    /** Set idle timeout for threads */
    public void setIdleTimeout(int idleTimeout);
    
    /** Set thread name prefix */
    public void setName(String name);
    
    /** Set daemon thread flag */
    public void setDaemon(boolean daemon);
    
    /** Get queue size */
    public int getQueueSize();
    
    /** Get busy thread count */
    public int getBusyThreads();
}

Usage Examples:

import org.eclipse.jetty.util.thread.QueuedThreadPool;

// Create and configure thread pool
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMinThreads(5);
threadPool.setMaxThreads(50);
threadPool.setIdleTimeout(30000); // 30 seconds
threadPool.setName("MyApp");
threadPool.setDaemon(false);

// Start thread pool
threadPool.start();

// Submit tasks
threadPool.execute(() -> {
    System.out.println("Task running in: " + Thread.currentThread().getName());
});

// Monitor thread pool
System.out.println("Threads: " + threadPool.getThreads());
System.out.println("Idle: " + threadPool.getIdleThreads());
System.out.println("Busy: " + threadPool.getBusyThreads());
System.out.println("Queue: " + threadPool.getQueueSize());

// Shutdown
threadPool.stop();

VirtualThreadPool

Virtual thread pool implementation for Java 19+ virtual threads.

/**
 * Virtual thread pool implementation (Java 19+)
 */
public class VirtualThreadPool extends ContainerLifeCycle implements ThreadPool, TryExecutor {
    /** Create virtual thread pool */
    public VirtualThreadPool();
    
    /** Create virtual thread pool with name */
    public VirtualThreadPool(String name);
    
    /** Try to execute task immediately */
    public boolean tryExecute(Runnable task);
}

Scheduler Interface

Task scheduling interface for delayed and recurring tasks.

/**
 * Task scheduler interface
 */
public interface Scheduler extends LifeCycle {
    /** Schedule one-time task */
    Task schedule(Runnable task, long delay, TimeUnit unit);
    
    /** Schedule recurring task with fixed delay */
    Task scheduleWithFixedDelay(Runnable task, long initialDelay, long delay, TimeUnit unit);
    
    /** Scheduled task interface */
    interface Task {
        boolean cancel();
        boolean isCancelled();
        boolean isDone();
    }
}

Scheduler Implementations

/**
 * ScheduledExecutorService-based scheduler
 */
public class ScheduledExecutorScheduler extends ContainerLifeCycle implements Scheduler {
    /** Create scheduler with default pool size */
    public ScheduledExecutorScheduler();
    
    /** Create scheduler with specific pool size */
    public ScheduledExecutorScheduler(String name, boolean daemon, int threads);
}

/**
 * Timer-based scheduler
 */
public class TimerScheduler extends ContainerLifeCycle implements Scheduler {
    /** Create timer scheduler */
    public TimerScheduler();
    
    /** Create timer scheduler with name and daemon flag */
    public TimerScheduler(String name, boolean daemon);
}

Usage Examples:

import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler;
import org.eclipse.jetty.util.thread.Scheduler;

// Create and start scheduler
Scheduler scheduler = new ScheduledExecutorScheduler("MyScheduler", true, 2);
scheduler.start();

// Schedule one-time task
Scheduler.Task task = scheduler.schedule(
    () -> System.out.println("Delayed task executed"),
    5, TimeUnit.SECONDS
);

// Schedule recurring task
Scheduler.Task recurring = scheduler.scheduleWithFixedDelay(
    () -> System.out.println("Recurring task: " + new Date()),
    1, 10, TimeUnit.SECONDS
);

// Cancel tasks
boolean cancelled = task.cancel();

// Shutdown
scheduler.stop();

ExecutionStrategy

Thread execution strategies for different workloads.

/**
 * Execution strategy interface
 */
public interface ExecutionStrategy extends LifeCycle {
    /** Produce and consume tasks */
    void produce();
    
    /** Dispatch execution */
    void dispatch();
    
    /** Producer interface for generating tasks */
    interface Producer {
        Runnable produce();
    }
}

Concurrency Utilities

/**
 * Auto-closeable lock implementation
 */
public class AutoLock implements Closeable {
    /** Create auto lock */
    public AutoLock();
    
    /** Acquire lock (use with try-with-resources) */
    public AutoLock lock();
    
    /** Try to acquire lock */
    public AutoLock tryLock();
    
    /** Check if locked */
    public boolean isLocked();
}

/**
 * Serialized task execution
 */
public class SerializedExecutor implements Executor {
    /** Create serialized executor */
    public SerializedExecutor(Executor executor);
}

/**
 * Reserved thread executor for low-latency tasks
 */
public class ReservedThreadExecutor extends AbstractLifeCycle implements TryExecutor {
    /** Create with thread pool and capacity */
    public ReservedThreadExecutor(Executor executor, int capacity);
    
    /** Set reservation capacity */
    public void setCapacity(int capacity);
    
    /** Get current capacity */
    public int getCapacity();
}

Usage Examples:

import org.eclipse.jetty.util.thread.AutoLock;
import org.eclipse.jetty.util.thread.SerializedExecutor;
import org.eclipse.jetty.util.thread.ReservedThreadExecutor;

// Auto lock usage
AutoLock lock = new AutoLock();
try (AutoLock ignored = lock.lock()) {
    // Critical section
    sharedResource.modify();
} // Lock automatically released

// Serialized execution
Executor serialized = new SerializedExecutor(threadPool);
serialized.execute(task1); // These tasks will execute
serialized.execute(task2); // in submission order
serialized.execute(task3);

// Reserved threads for low-latency
ReservedThreadExecutor reserved = new ReservedThreadExecutor(threadPool, 2);
reserved.start();

// Try immediate execution (low-latency path)
boolean executed = reserved.tryExecute(() -> {
    // High-priority, low-latency task
});

if (!executed) {
    // Fall back to regular thread pool
    threadPool.execute(task);
}

Performance Characteristics

  • QueuedThreadPool: Dynamic thread creation/destruction based on load
  • VirtualThreadPool: Millions of lightweight virtual threads (Java 19+)
  • ScheduledExecutorScheduler: Precise timing with thread pool backing
  • ReservedThreadExecutor: Ultra-low latency for critical tasks
  • SerializedExecutor: Ordered execution with minimal overhead
  • AutoLock: Zero-allocation lock with try-with-resources support

Best Practices

  1. Use QueuedThreadPool as default for most server applications
  2. Configure min/max threads based on expected load
  3. Monitor thread pool metrics (busy, idle, queue size)
  4. Use VirtualThreadPool for I/O-heavy workloads on Java 19+
  5. Reserve threads for critical, low-latency operations
  6. Serialize related tasks to avoid synchronization overhead
  7. Use AutoLock for clean, exception-safe locking patterns

Install with Tessl CLI

npx tessl i tessl/maven-org-eclipse-jetty--jetty-util

docs

async-operations.md

component-lifecycle.md

core-utilities.md

data-structures.md

index.md

resource-management.md

security.md

statistics.md

threading.md

tile.json