Aufgabe 3 bearbeitet

This commit is contained in:
romanschenk37 2022-03-06 22:04:31 +01:00
parent b9e8fb0665
commit 6bd7f43163
2 changed files with 102 additions and 9 deletions

View File

@ -11,6 +11,7 @@ import java.util.concurrent.*;
public class MandelbrotCallableProcessor extends MandelbrotProcessor { public class MandelbrotCallableProcessor extends MandelbrotProcessor {
private volatile boolean terminate; // signal the threads to abort processing and terminate private volatile boolean terminate; // signal the threads to abort processing and terminate
ExecutorService executorService;
/** /**
* Initialize the Mandelbrot processor. * Initialize the Mandelbrot processor.
@ -38,9 +39,13 @@ public class MandelbrotCallableProcessor extends MandelbrotProcessor {
super.startTime = System.currentTimeMillis(); super.startTime = System.currentTimeMillis();
// TODO: Start the the executor service with the given number of threads. // 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 // TODO: process all rows using the Callable MandelbrotTask and store returned Futures in a list
List<Future<ImageRow>> 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) // TODO: get results from Future list and send them to the processListener (GUI)
// make sure to handle all Exceptions // make sure to handle all Exceptions
try { try {
for(Future<ImageRow> future : futures) {
processorListener.rowProcessed(future.get());
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally { } finally {
// stop processing and shutdown executor // stop processing and shutdown executor
stopProcessing(); stopProcessing();
@ -70,7 +78,7 @@ public class MandelbrotCallableProcessor extends MandelbrotProcessor {
public void stopProcessing() { public void stopProcessing() {
terminate = true; // signal the threads to abort terminate = true; // signal the threads to abort
// TODO: shutdown executor service // TODO: shutdown executor service
executorService.shutdown();
// calculate processing time // calculate processing time
@ -93,10 +101,49 @@ public class MandelbrotCallableProcessor extends MandelbrotProcessor {
private class MandelbrotTask implements Callable<ImageRow> { private class MandelbrotTask implements Callable<ImageRow> {
// TODO: Use Task implementation from MandelbrotExecutorProcessor change it to a 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() { MandelbrotTask(int row) {
return null; // Compute one row of pixels. 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 } // end MandelbrotTask
} }

View File

@ -10,7 +10,7 @@ import java.util.concurrent.Executors;
public class MandelbrotExecutorProcessor extends MandelbrotProcessor { public class MandelbrotExecutorProcessor extends MandelbrotProcessor {
private volatile boolean terminate; // signal the threads to abort processing and terminate private volatile boolean terminate; // signal the threads to abort processing and terminate
private ExecutorService executorService;
/** /**
* Initialize the Mandelbrot processor. * 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. // TODO: Start the the executor service with the given number of threads.
executorService = Executors.newFixedThreadPool(numThreads);
// TODO: start a task for each row // 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() { public void stopProcessing() {
terminate = true; // signal the threads to abort terminate = true; // signal the threads to abort
// TODO: shutdown executor service // TODO: shutdown executor service
executorService.shutdown();
// calculate processing time // calculate processing time
long duration = System.currentTimeMillis() - startTime; long duration = System.currentTimeMillis() - startTime;
@ -72,9 +82,45 @@ public class MandelbrotExecutorProcessor extends MandelbrotProcessor {
private class MandelbrotTask implements Runnable { private class MandelbrotTask implements Runnable {
// TODO: Use Task implementation from MandelbrotTaskProcessor and modify it to calculat only one row. // 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 @Override
public void run() { 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 } // end MandelbrotTask
} }