Skip to content

feat: add Parallel.race() — first successful result wins#22

Closed
praveer-rai wants to merge 3 commits intomainfrom
feat/race
Closed

feat: add Parallel.race() — first successful result wins#22
praveer-rai wants to merge 3 commits intomainfrom
feat/race

Conversation

@praveer-rai
Copy link
Copy Markdown
Contributor

@praveer-rai praveer-rai commented Apr 18, 2026

Runs all suppliers concurrently and returns the first successful result. Remaining tasks are interrupted. If all fail, the last failure is rethrown.

var result = parallel.race(
    () -> fetchFromPrimary(),
    () -> fetchFromReplica()
);

Useful for hedged requeststry multiple paths, take whichever responds first. Supports varargs and List<Supplier<T>> overloads. Respects withMaxConcurrency and context propagation.

Test plan

- Returns first successful result, ignores slower tasks
- Interrupts remaining tasks after first success
- Succeeds if some tasks fail but at least one succeeds
- Throws when all tasks fail
- Rejects empty task list
- Propagates MDC context
- README updated

Runs all suppliers concurrently on virtual threads. Returns the result
of the first to complete successfully, then interrupts the rest. If all
fail, rethrows the last failure. Supports varargs and List overloads.
@jimbao
Copy link
Copy Markdown
Contributor

jimbao commented Apr 21, 2026

Lets consider when a practical use case appears

@jimbao jimbao closed this Apr 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants