From 5673c99d4e7a18f036ffb7244b5ebce887b24671 Mon Sep 17 00:00:00 2001 From: Nilstrieb Date: Sun, 22 Nov 2020 10:48:22 +0100 Subject: [PATCH] fixed some multithreading bugs --- .../CalculationThread.java | 37 +++++++++++++------ src/mandelbrotCalculator/MandelbrotSet.java | 10 +++-- 2 files changed, 33 insertions(+), 14 deletions(-) diff --git a/src/mandelbrotCalculator/CalculationThread.java b/src/mandelbrotCalculator/CalculationThread.java index 600085f..42d40ef 100644 --- a/src/mandelbrotCalculator/CalculationThread.java +++ b/src/mandelbrotCalculator/CalculationThread.java @@ -23,11 +23,14 @@ public class CalculationThread implements Runnable { double[][] zoomValues; CNumber[][] samples; - MandelbrotSet set; - Controller controller; + final MandelbrotSet set; + final Controller controller; - public void start(){ - if(thread == null){ + public void start() { + if (thread == null) { + synchronized (controller){ + controller.printOutput("Thread " + threadNumber + " started"); + } thread = new Thread(this, String.valueOf(threadNumber)); thread.start(); } @@ -64,19 +67,29 @@ public class CalculationThread implements Runnable { createImage(image, frameCounter, width, height, values, iterations); - set.frameFinished(frameCounter); + synchronized (set) { + set.frameFinished(frameCounter); + } long frameTime = System.currentTimeMillis() - startTime; System.out.println("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); - controller.printOutput("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); + + synchronized (controller) { + controller.printOutput("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); + } } long totalTime = System.currentTimeMillis() - totalStartTime; System.out.println("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); - controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); - set.setFinished(threadNumber); + synchronized (controller) { + controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); + } + + synchronized (set) { + set.setFinished(threadNumber); + } } @@ -95,12 +108,13 @@ public class CalculationThread implements Runnable { /** * Creates an image from the calculated values + * * @param image the image to be used * @param counter the frame number of the image * @param width width of the image * @param height height of the image * @param values the values of every pixel - * @param iterations the amount of interations + * @param iterations the amount of iterations */ void createImage(BufferedImage image, int counter, int width, int height, double[][] values, int iterations) { @@ -131,9 +145,10 @@ public class CalculationThread implements Runnable { /** * Checks whether the number is in the Mandelbrot set - * @param number The Complex Number to be checked + * + * @param number The Complex Number to be checked * @param iterations The amount of iterations the program should do - * @param threshold The threshold for a number not being in the set + * @param threshold The threshold for a number not being in the set * @return -1 if the number is in the set, else it returns the amount of interations before it reached the threshold */ int checkMandelbrot(CNumber number, int iterations, double threshold) { diff --git a/src/mandelbrotCalculator/MandelbrotSet.java b/src/mandelbrotCalculator/MandelbrotSet.java index e59363b..8395173 100644 --- a/src/mandelbrotCalculator/MandelbrotSet.java +++ b/src/mandelbrotCalculator/MandelbrotSet.java @@ -31,7 +31,7 @@ public class MandelbrotSet { private long startTime = System.currentTimeMillis(); - private Controller controller; + private final Controller controller; /** @@ -163,13 +163,17 @@ public class MandelbrotSet { if(finished){ System.out.println("CALCULATION FINISHED"); - controller.printOutput("CALCULATION FINISHED"); + synchronized (controller) { + controller.printOutput("CALCULATION FINISHED"); + } // TIME should probably not be here and serves no practical purpose but that doesn't stop me from keeping it here long endTime = System.currentTimeMillis(); long completionTimeLong = endTime - startTime; double completionTimeSec = (double) completionTimeLong / 1000.0; System.out.println("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); - controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); + synchronized (controller) { + controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); + } } }