forked from Parallele_Programmierung/Labs
Add structured concurrency example for prime generation (classless,
instance-main, IO.println)main
parent
9329f962f6
commit
6e129de66c
|
|
@ -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));
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue