From 6bd7f431636de4da344a46dc5b5c2e78f23a5991 Mon Sep 17 00:00:00 2001 From: romanschenk37 <84532681+romanschenk37@users.noreply.github.com> Date: Sun, 6 Mar 2022 22:04:31 +0100 Subject: [PATCH] Aufgabe 3 bearbeitet --- .../MandelbrotCallableProcessor.java | 63 ++++++++++++++++--- .../MandelbrotExecutorProcessor.java | 48 +++++++++++++- 2 files changed, 102 insertions(+), 9 deletions(-) diff --git a/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotCallableProcessor.java b/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotCallableProcessor.java index 56d9155..82d746c 100644 --- a/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotCallableProcessor.java +++ b/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotCallableProcessor.java @@ -11,6 +11,7 @@ import java.util.concurrent.*; public class MandelbrotCallableProcessor extends MandelbrotProcessor { private volatile boolean terminate; // signal the threads to abort processing and terminate + ExecutorService executorService; /** * Initialize the Mandelbrot processor. @@ -38,9 +39,13 @@ public class MandelbrotCallableProcessor extends MandelbrotProcessor { super.startTime = System.currentTimeMillis(); // TODO: Start the the executor service with the given number of threads. + executorService = Executors.newFixedThreadPool(numThreads); // TODO: process all rows using the Callable MandelbrotTask and store returned Futures in a list - + List> futures = new ArrayList<>(); + for(int row = 0; row < height; row ++) { + futures.add(executorService.submit(new MandelbrotTask(row))); + } @@ -51,11 +56,14 @@ public class MandelbrotCallableProcessor extends MandelbrotProcessor { // TODO: get results from Future list and send them to the processListener (GUI) // make sure to handle all Exceptions try { + for(Future future : futures) { + processorListener.rowProcessed(future.get()); + } - - - - + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); } finally { // stop processing and shutdown executor stopProcessing(); @@ -70,7 +78,7 @@ public class MandelbrotCallableProcessor extends MandelbrotProcessor { public void stopProcessing() { terminate = true; // signal the threads to abort // TODO: shutdown executor service - + executorService.shutdown(); // calculate processing time @@ -93,10 +101,49 @@ public class MandelbrotCallableProcessor extends MandelbrotProcessor { private class MandelbrotTask implements Callable { // TODO: Use Task implementation from MandelbrotExecutorProcessor change it to a Callable. + private final int row; + private final double xmin, xmax, ymin, ymax, dx, dy; + private final int maxIterations; - public ImageRow call() { - return null; // Compute one row of pixels. + MandelbrotTask(int row) { + this.row = row; + xmin = -1.6744096740931858; + xmax = -1.674409674093473; + ymin = 4.716540768697223E-5; + ymax = 4.716540790246652E-5; + dx = (xmax - xmin) / (width - 1); + dy = (ymax - ymin) / (height - 1); + maxIterations = 10000; } + @Override + public ImageRow call() { + // Compute one row of pixels. + + final ImageRow imageRow = new ImageRow(row, width); + double x; + double y = ymax - dy * row; + + for (int col = 0; col < width; col++) { + x = xmin + dx * col; + int count = 0; + double xx = x; + double yy = y; + while (count < maxIterations && (xx * xx + yy * yy) < 4) { + count++; + double newxx = xx * xx - yy * yy + x; + yy = 2 * xx * yy + y; + xx = newxx; + } + // select color based on count of iterations + imageRow.pixels[col] = (count != maxIterations) ? + palette[count % palette.length] : Color.BLACK; + + } + return imageRow; + } + + + } // end MandelbrotTask } diff --git a/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotExecutorProcessor.java b/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotExecutorProcessor.java index 3c01375..b83bf14 100644 --- a/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotExecutorProcessor.java +++ b/code/Mandelbrot/src/main/java/ch/zhaw/prog2/mandelbrot/processors/MandelbrotExecutorProcessor.java @@ -10,7 +10,7 @@ import java.util.concurrent.Executors; public class MandelbrotExecutorProcessor extends MandelbrotProcessor { private volatile boolean terminate; // signal the threads to abort processing and terminate - + private ExecutorService executorService; /** * Initialize the Mandelbrot processor. @@ -39,8 +39,17 @@ public class MandelbrotExecutorProcessor extends MandelbrotProcessor { // TODO: Start the the executor service with the given number of threads. + executorService = Executors.newFixedThreadPool(numThreads); + // TODO: start a task for each row + for(int row = 0; row < height; row ++) { + + executorService.execute(new MandelbrotExecutorProcessor.MandelbrotTask(row)); + + } + + @@ -54,6 +63,7 @@ public class MandelbrotExecutorProcessor extends MandelbrotProcessor { public void stopProcessing() { terminate = true; // signal the threads to abort // TODO: shutdown executor service + executorService.shutdown(); // calculate processing time long duration = System.currentTimeMillis() - startTime; @@ -72,9 +82,45 @@ public class MandelbrotExecutorProcessor extends MandelbrotProcessor { private class MandelbrotTask implements Runnable { // TODO: Use Task implementation from MandelbrotTaskProcessor and modify it to calculat only one row. + private final int row; + private final double xmin, xmax, ymin, ymax, dx, dy; + private final int maxIterations; + + MandelbrotTask(int row) { + this.row = row; + xmin = -1.6744096740931858; + xmax = -1.674409674093473; + ymin = 4.716540768697223E-5; + ymax = 4.716540790246652E-5; + dx = (xmax - xmin) / (width - 1); + dy = (ymax - ymin) / (height - 1); + maxIterations = 10000; + } + @Override public void run() { + final ImageRow imageRow = new ImageRow(row, width); + double x; + double y = ymax - dy * row; + for (int col = 0; col < width; col++) { + x = xmin + dx * col; + int count = 0; + double xx = x; + double yy = y; + while (count < maxIterations && (xx * xx + yy * yy) < 4) { + count++; + double newxx = xx * xx - yy * yy + x; + yy = 2 * xx * yy + y; + xx = newxx; + } + // select color based on count of iterations + imageRow.pixels[col] = (count != maxIterations) ? + palette[count % palette.length] : Color.BLACK; + + } + processorListener.rowProcessed(imageRow); + taskFinished(); } } // end MandelbrotTask }