Add structured concurrency example for prime generation (classless,

instance-main, IO.println)
main
Sandro Leuchter 2026-06-20 09:24:08 +00:00
parent 9329f962f6
commit 6e129de66c
1 changed files with 14 additions and 0 deletions

View File

@ -0,0 +1,14 @@
import java.util.concurrent.StructuredTaskScope.Joiner;
BigInteger parallelPrime(int bitLength) throws InterruptedException {
try (var scope = StructuredTaskScope.open(Joiner.<BigInteger>anySuccessfulResultOrThrow())) {
for (var i = 0; i < ForkJoinPool.commonPool().getParallelism(); i++) {
scope.fork(() -> BigInteger.probablePrime(bitLength, ThreadLocalRandom.current()));
}
return scope.join();
}
}
void main() throws InterruptedException {
IO.println(parallelPrime(4096));
}