package example.obrh;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/** Java 17 optional blocking-worker example; one managed instance per isolation domain. */
public final class BoundedBlockingExecutor {
    private BoundedBlockingExecutor() { }

    public static ThreadPoolExecutor create() {
        return new ThreadPoolExecutor(8, 8, 0L, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(16),
                new ThreadPoolExecutor.AbortPolicy());
    }
    // RejectedExecutionException must map to a deliberate overload response.
    // Do not use CallerRunsPolicy: it can move provider stalls back onto Tomcat workers.
    // Submission requires logical admission and a finite remaining deadline.
    // Acquire attempt permits in the worker immediately before scarce-resource waits.
    // Observe queue delay separately; remove/cancel queued work when its deadline ends.
    // Release active permits only when the worker actually exits, not when Future.cancel
    // returns. Interruption does not guarantee that an underlying provider call stops.
    // Propagate bounded trace/security/tenant context explicitly; clear it in finally.
    // Owner drains/shuts down this executor as part of the application lifecycle.
}
