Aufgabe 2.2 bearbeitet
This commit is contained in:
parent
91aeee07fb
commit
b9e8fb0665
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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<Future<PrimeTaskCallable.Result>> 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<PrimeTaskCallable.Result> 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() {
|
||||
|
|
|
@ -12,7 +12,7 @@ public class PrimeTaskCallable implements Callable<PrimeTaskCallable.Result> {
|
|||
|
||||
|
||||
public Result call() {
|
||||
return null;
|
||||
return new Result(primeCandidate, findSmallestFactor(primeCandidate));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -47,5 +47,14 @@ public class PrimeTaskCallable implements Callable<PrimeTaskCallable.Result> {
|
|||
this.factor = factor;
|
||||
this.isPrime = factor == 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Result{" +
|
||||
"candidate=" + candidate +
|
||||
", factor=" + factor +
|
||||
", isPrime=" + isPrime +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue