Initial commit
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Gradle build configuration for specific lab module / exercise
|
||||
*/
|
||||
// the Java plugin is added by default in the main lab configuration
|
||||
plugins {
|
||||
id 'java'
|
||||
}
|
||||
|
||||
// Project/Module information
|
||||
description = 'Lab01 PrimeChecker Solution'
|
||||
group = 'ch.zhaw.prog2'
|
||||
version = '2022.1'
|
||||
|
||||
// Dependency configuration
|
||||
// repositories to download dependencies from
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
// required dependencies
|
||||
dependencies {
|
||||
|
||||
}
|
||||
|
||||
// Java plugin configuration
|
||||
java {
|
||||
// By default the Java version of the gradle process is used as source/target version.
|
||||
// This can be overridden, to ensure a specific version. Enable only if required.
|
||||
// sourceCompatibility = JavaVersion.VERSION_17 // ensure Java source code compatibility
|
||||
// targetCompatibility = JavaVersion.VERSION_17 // version of the created byte-code
|
||||
|
||||
// Java compiler specific options
|
||||
compileJava {
|
||||
// source files should be UTF-8 encoded
|
||||
options.encoding = 'UTF-8'
|
||||
// for more options see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.compile.CompileOptions.html
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package ch.zhaw.prog2.primechecker;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PrimeChecker {
|
||||
|
||||
private static final long LOWER_LIMIT = 10000L;
|
||||
private static final long UPPER_LIMIT = 1000000000L;
|
||||
private static final int NUM_PRIME = 500;
|
||||
|
||||
public static void main(String[] args) {
|
||||
long starttime = System.currentTimeMillis();
|
||||
long duration;
|
||||
try {
|
||||
checkPrimes(NUM_PRIME);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Interrupted - " + e.getMessage());
|
||||
} finally {
|
||||
duration = System.currentTimeMillis() - starttime;
|
||||
}
|
||||
System.out.println("Finished in " + duration + " ms");
|
||||
}
|
||||
|
||||
private static void checkPrimes(int numPrimes) throws InterruptedException {
|
||||
List<Thread> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < numPrimes; i++) {
|
||||
//new PrimeTask(nextRandom()).run(); // runs sequential in current thread
|
||||
Thread task = new Thread(new PrimeTask(nextRandom()));
|
||||
tasks.add(task);
|
||||
task.start();
|
||||
}
|
||||
for (Thread task: tasks) {
|
||||
task.join();
|
||||
}
|
||||
}
|
||||
|
||||
private static long nextRandom() {
|
||||
return LOWER_LIMIT + (long)(Math.random() * (UPPER_LIMIT - LOWER_LIMIT));
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package ch.zhaw.prog2.primechecker;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class PrimeCheckerExecutor {
|
||||
|
||||
private static final long LOWER_LIMIT = 10000L;
|
||||
private static final long UPPER_LIMIT = 1000000000L;
|
||||
private static final int NUM_PRIME = 500;
|
||||
private static StringWriter statsWriter = new StringWriter();
|
||||
private static PrintWriter stats = new PrintWriter(statsWriter);
|
||||
|
||||
public static void main(String[] args) {
|
||||
long starttime = System.currentTimeMillis();
|
||||
long duration;
|
||||
try {
|
||||
checkPrimes(NUM_PRIME);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Interrupted - " + e.getMessage());
|
||||
} finally {
|
||||
duration = System.currentTimeMillis() - starttime;
|
||||
}
|
||||
System.out.println("Finished in " + duration + " ms");
|
||||
}
|
||||
|
||||
private static void checkPrimes(int numPrimes) throws InterruptedException {
|
||||
// determine number of cores
|
||||
int numCores = Runtime.getRuntime().availableProcessors();
|
||||
// create ExecutorService
|
||||
ExecutorService executor = Executors.newFixedThreadPool(numCores);
|
||||
// ExecutorService executor = Executors.newCachedThreadPool();
|
||||
|
||||
for (int i = 0; i < numPrimes; i++) {
|
||||
// execute the runnable using the executor service
|
||||
executor.execute(new PrimeTask(nextRandom()));
|
||||
// collect executor statistics every 100 tasks
|
||||
if (i % 100 == 0) stats.println("Executor Info after " + i + " tasks: " + executor);
|
||||
}
|
||||
// stop ExecutorService
|
||||
executor.shutdown();
|
||||
// wait for termination with timeout of 1 minute
|
||||
executor.awaitTermination(1, TimeUnit.MINUTES);
|
||||
stats.println("Executor Info after termination: " + executor);
|
||||
// print executor statistics
|
||||
System.out.print(statsWriter.toString());
|
||||
}
|
||||
|
||||
private static long nextRandom() {
|
||||
return LOWER_LIMIT + (long)(Math.random() * (UPPER_LIMIT - LOWER_LIMIT));
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package ch.zhaw.prog2.primechecker;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.*;
|
||||
|
||||
public class PrimeCheckerFuture {
|
||||
|
||||
private static final long LOWER_LIMIT = 10000L;
|
||||
private static final long UPPER_LIMIT = 1000000000L;
|
||||
private static final int NUM_PRIME = 500;
|
||||
|
||||
public static void main(String[] args) {
|
||||
long starttime = System.currentTimeMillis();
|
||||
long duration;
|
||||
try {
|
||||
checkPrimes(NUM_PRIME);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("Interrupted - " + e.getMessage());
|
||||
} finally {
|
||||
duration = System.currentTimeMillis() - starttime;
|
||||
}
|
||||
System.out.println("Finished in " + duration + " ms");
|
||||
}
|
||||
|
||||
private static void checkPrimes(int numPrimes) throws InterruptedException {
|
||||
// create ExecutorService
|
||||
int numCores = Runtime.getRuntime().availableProcessors();
|
||||
ExecutorService executor = Executors.newFixedThreadPool(numCores);
|
||||
// submit tasks to ExecutorService and collect the returned Futures in a List
|
||||
List<Future<PrimeTaskCallable.Result>> tasks = new ArrayList<>();
|
||||
for (int i = 0; i < numPrimes; i++) {
|
||||
// execute runnable / submit callable
|
||||
tasks.add(executor.submit(new PrimeTaskCallable(nextRandom())));
|
||||
}
|
||||
// Loop through List, wait for completion and print results
|
||||
for (Future<PrimeTaskCallable.Result> task : tasks) {
|
||||
try {
|
||||
PrimeTaskCallable.Result result = task.get();
|
||||
System.out.println("Number: " + result.candidate + " -> " +
|
||||
(result.isPrime ? "PRIME" : "Factor: " + result.factor));
|
||||
} catch (ExecutionException e) {
|
||||
System.out.println("Execution failed : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
// stop ExecutorService
|
||||
executor.shutdown();
|
||||
// await termination with timeout 1 minute
|
||||
executor.awaitTermination(1, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
private static long nextRandom() {
|
||||
return LOWER_LIMIT + (long)(Math.random() * (UPPER_LIMIT - LOWER_LIMIT));
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package ch.zhaw.prog2.primechecker;
|
||||
|
||||
public class PrimeTask implements Runnable {
|
||||
|
||||
private final long primeCandidate;
|
||||
|
||||
public PrimeTask(long primeCandidate) {
|
||||
this.primeCandidate = primeCandidate;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
long smallestFactor = findSmallestFactor(primeCandidate);
|
||||
System.out.println("Number: " + this.primeCandidate + " -> " +
|
||||
( smallestFactor == 0 ? "PRIME" : "Factor: " + smallestFactor ));
|
||||
}
|
||||
/**
|
||||
* Brute force check if submitted candidate is a prime number.
|
||||
*
|
||||
* @param primeCandidate Number to check if it is a
|
||||
* @return 0 if prime number, smallest factor otherwise
|
||||
*/
|
||||
private long findSmallestFactor(long primeCandidate) {
|
||||
if (primeCandidate>3) {
|
||||
for(long factor = 2; factor <=primeCandidate/2; ++factor) {
|
||||
if ( primeCandidate / factor * factor == primeCandidate) {
|
||||
return factor; // found a factor -> is no prime
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0; // is prime number
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package ch.zhaw.prog2.primechecker;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class PrimeTaskCallable implements Callable<PrimeTaskCallable.Result> {
|
||||
|
||||
private final long primeCandidate;
|
||||
|
||||
public PrimeTaskCallable(long primeCandidate) {
|
||||
this.primeCandidate = primeCandidate;
|
||||
}
|
||||
|
||||
public Result call() {
|
||||
return new Result(primeCandidate, findSmallestFactor(primeCandidate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Brute force check if submitted candidate is a prime number.
|
||||
*
|
||||
* @param primeCandidate Number to check if it is a
|
||||
* @return 0 if prime number, smallest factor otherwise
|
||||
*/
|
||||
private long findSmallestFactor(long primeCandidate) {
|
||||
if (primeCandidate>3) {
|
||||
for(long factor = 2; factor <= primeCandidate/2; ++factor) {
|
||||
if ( primeCandidate / factor * factor == primeCandidate) {
|
||||
return factor; // found a factor -> is no prime
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0; // is prime number
|
||||
}
|
||||
|
||||
/**
|
||||
* Small static helper class serving as a container to return the result.
|
||||
* No accessor methods. Use direct access to fields to read values.
|
||||
* (Starting from Java14, we could use Java Records for this)
|
||||
*/
|
||||
public static class Result {
|
||||
public final long candidate;
|
||||
public final long factor;
|
||||
public final boolean isPrime;
|
||||
|
||||
public Result(long candidate, long factor) {
|
||||
this.candidate = candidate;
|
||||
this.factor = factor;
|
||||
this.isPrime = factor == 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user