ConcurrencyHard

The tasks that failed and told nobody

A nightly job pushes several thousand records through a thread pool. It reports success every night. Support keeps finding records that were never processed, and there is nothing in the logs.

Make failures observable: after the run, the job must know how many tasks threw and how many succeeded.

Example

input10 tasks, 3 of which throwoutputsucceeded = 7, failed = 3

Every task is accounted for. No exception may be lost.

Constraints

  • run() must not return until every task has finished.
  • A task that throws must be counted as failed, and must not stop the others.
  • Counts must be correct when several threads finish at once.

Hints

Hint 1
ExecutorService.submit() does not let an exception escape. It captures it in the Future and returns.
Hint 2
Nothing here ever looks at those Futures, so the exception is stored and then discarded — that is why the logs are empty.
Hint 3
Either keep the Futures and call get() on each, or catch inside the task yourself. execute() is the other route: it has no Future to hide anything in.

Stuck? The lesson behind this problem: 🧵 Executors and thread pools

java
Tab indents · Escape first to tab out

Test cases

These are the specification. Run tests checks your answer against them.

CaseInputExpected
three of ten tasks throwsucceeded = 7, failed = 3
every task throwssucceeded = 0, failed = 6
nothing throwssucceeded = 12, failed = 0
counts survive concurrencysucceeded + failed = 200