Subtractive Design: parallel-in-scope Design Review
This page is a condensed English overview. The long-form Chinese design review under
docs/zh/design/philosophy.md(published at/zh/design/philosophy/) carries the full reasoning and is the authoritative version for design tradeoffs.
Introduction
The library targets three recurring Java 8 problems: cancellation that cannot reach CPU-bound work, context lost across thread pools, and deadlocks caused by nested work submitted to a bounded pool. Its design is intentionally narrow: batch execution with explicit cancellation and diagnostics, rather than a general asynchronous workflow framework.
The central API: Par.map
Par.map gives a batch a clear scope, executor, parallelism, timeout, and result report. The scope makes failure and cancellation ownership visible. It also gives the implementation one place to enforce fail-fast behavior, sliding-window submission, and parent-child cancellation.
Prepare, bind, then submit
Every element has its execution future prepared before cancellation and timeout wiring. Binding finishes before any task is submitted, including tasks that an executor runs on the caller. The sliding window controls admission of those same prepared futures; there are no placeholders or later delegate swaps. Public execution waits for terminal results and bounded cleanup, then returns frozen values and failures.
Two cancellation exceptions
Normal cancellation is a control-flow event, so LeanCancellationException avoids collecting a stack trace. The library raises LeanCancellationException; code that throws its own CancellationException keeps its stack trace, so no redundant framework subtype is needed.
Sliding-window scheduling
Submitting every item at once can flood a queue and make nested calls deadlock. SlidingWindowSubmitter keeps only a bounded window of work in flight. Only an explicit rejectEnqueue makes SubmissionAwareBlockingQueue refuse a task, and no task label selects an executor or changes scheduling, so queue policy does not change the structured scope.
Explicit execution context and deadlock visibility
TaskExecutionContext is installed only while a scoped task executes; its batch context defines
the current task's cancellation and nesting ownership. A separate internal submission scope exists
only while work is handed to an executor, so SubmissionAwareBlockingQueue can apply batch enqueue policy
before a task starts. Neither context is relayed through arbitrary user executor submissions.
Every ParRuntime records which of its executors synchronously wait for which, always on, so the
library can report pool wait cycles — including ones formed by separate requests — that a thread
dump would otherwise reveal only after production impact. This is executor deadlock detection, not a
replacement for lock analysis.
Deliberate boundaries
The project does not add open-ended failure policies beyond its two join policies (fail-fast by default, await-all), a built-in retry engine, a Spring Boot starter, or a second fluent-composition API. Those features increase surface area and overlap with mature libraries. Callers can compose the batch API with Guava or CompletableFuture at their application boundary.
Closing principle
The useful abstraction is the smallest one that makes failure ownership, cancellation, scheduling, and diagnostics explicit. More APIs are not automatically more capability if they make those guarantees harder to reason about.