From b9e8fb06657caaf0ca67f496d2545822324bd9ba Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Thu, 3 Mar 2022 16:54:15 +0100 Subject: [PATCH] Aufgabe 2.2 bearbeitet --- .../zhaw/prog2/primechecker/PrimeChecker.java | 2 +- .../primechecker/PrimeCheckerExecutor.java | 12 +++++++----- .../prog2/primechecker/PrimeCheckerFuture.java | 17 ++++++++++++----- .../prog2/primechecker/PrimeTaskCallable.java | 11 ++++++++++- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeChecker.java b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeChecker.java index 76e6e42..9036ecf 100644 --- a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeChecker.java +++ b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeChecker.java @@ -21,7 +21,7 @@ public class PrimeChecker { private static void checkPrimes(int numPrimes) throws InterruptedException { for (int i = 0; i < numPrimes; i++) { - new PrimeTask(nextRandom()).run(); // runs sequential in current thread + new Thread(new PrimeTask(nextRandom())).start(); // runs sequential in current thread } } diff --git a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerExecutor.java b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerExecutor.java index aa390c4..423d0c2 100644 --- a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerExecutor.java +++ b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerExecutor.java @@ -2,6 +2,7 @@ package ch.zhaw.prog2.primechecker; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class PrimeCheckerExecutor { @@ -25,15 +26,16 @@ public class PrimeCheckerExecutor { private static void checkPrimes(int numPrimes) throws InterruptedException { // TODO: create ExecutorService - What ThreadPool-Type/-Size fits best? - + ExecutorService executor = Executors.newFixedThreadPool(12); for (int i = 0; i < numPrimes; i++) { // TODO: execute the runnable using the executor service + executor.execute(new PrimeTask(nextRandom())); } - // stop ExecutorService - - // wait for termination with timeout of 1 minute - + // TODO: stop ExecutorService + executor.shutdown(); + // TODO: wait for termination with timeout of 1 minute + executor.awaitTermination(1, TimeUnit.MINUTES); } private static long nextRandom() { diff --git a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerFuture.java b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerFuture.java index a871611..53b24b3 100644 --- a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerFuture.java +++ b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeCheckerFuture.java @@ -25,19 +25,26 @@ public class PrimeCheckerFuture { private static void checkPrimes(int numPrimes) throws InterruptedException { // TODO: create ExecutorService - + ExecutorService executor = Executors.newFixedThreadPool(12); // TODO: submit tasks to ExecutorService and collect the returned Futures in a List + List> futures = new ArrayList<>(); for (int i = 0; i < numPrimes; i++) { - + futures.add(executor.submit(new PrimeTaskCallable(nextRandom()))); } // TODO: Loop through List, wait for completion and print results - + for(Future future : futures){ + try { + System.out.println(future.get()); + } catch (ExecutionException e) { + e.printStackTrace(); + } + } // TODO: stop ExecutorService - + executor.shutdown(); // TODO: await termination with timeout 1 minute - + executor.awaitTermination(1, TimeUnit.MINUTES); } private static long nextRandom() { diff --git a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeTaskCallable.java b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeTaskCallable.java index e29f293..a4a7f51 100644 --- a/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeTaskCallable.java +++ b/code/PrimeChecker/src/main/java/ch/zhaw/prog2/primechecker/PrimeTaskCallable.java @@ -12,7 +12,7 @@ public class PrimeTaskCallable implements Callable { public Result call() { - return null; + return new Result(primeCandidate, findSmallestFactor(primeCandidate)); } /** @@ -47,5 +47,14 @@ public class PrimeTaskCallable implements Callable { this.factor = factor; this.isPrime = factor == 0; } + + @Override + public String toString() { + return "Result{" + + "candidate=" + candidate + + ", factor=" + factor + + ", isPrime=" + isPrime + + '}'; + } } }