fixed some multithreading bugs

This commit is contained in:
nora 2020-11-22 10:48:22 +01:00
parent 580e3826b5
commit 5673c99d4e
2 changed files with 33 additions and 14 deletions

View file

@ -23,11 +23,14 @@ public class CalculationThread implements Runnable {
double[][] zoomValues; double[][] zoomValues;
CNumber[][] samples; CNumber[][] samples;
MandelbrotSet set; final MandelbrotSet set;
Controller controller; final Controller controller;
public void start(){ public void start() {
if(thread == null){ if (thread == null) {
synchronized (controller){
controller.printOutput("Thread " + threadNumber + " started");
}
thread = new Thread(this, String.valueOf(threadNumber)); thread = new Thread(this, String.valueOf(threadNumber));
thread.start(); thread.start();
} }
@ -64,19 +67,29 @@ public class CalculationThread implements Runnable {
createImage(image, frameCounter, width, height, values, iterations); createImage(image, frameCounter, width, height, values, iterations);
synchronized (set) {
set.frameFinished(frameCounter); set.frameFinished(frameCounter);
}
long frameTime = System.currentTimeMillis() - startTime; long frameTime = System.currentTimeMillis() - startTime;
System.out.println("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); System.out.println("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s");
synchronized (controller) {
controller.printOutput("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s"); controller.printOutput("Frame " + frameCounter + " finished in " + ((double) frameTime / 1000) + "s");
}
} }
long totalTime = System.currentTimeMillis() - totalStartTime; long totalTime = System.currentTimeMillis() - totalStartTime;
System.out.println("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s"); System.out.println("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s");
controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s");
synchronized (controller) {
controller.printOutput("--Thread " + threadNumber + " completed. Process took " + ((double) totalTime / 1000) + "s");
}
synchronized (set) {
set.setFinished(threadNumber); set.setFinished(threadNumber);
}
} }
@ -95,12 +108,13 @@ public class CalculationThread implements Runnable {
/** /**
* Creates an image from the calculated values * Creates an image from the calculated values
*
* @param image the image to be used * @param image the image to be used
* @param counter the frame number of the image * @param counter the frame number of the image
* @param width width of the image * @param width width of the image
* @param height height of the image * @param height height of the image
* @param values the values of every pixel * @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) { void createImage(BufferedImage image, int counter, int width, int height, double[][] values, int iterations) {
@ -131,6 +145,7 @@ public class CalculationThread implements Runnable {
/** /**
* Checks whether the number is in the Mandelbrot set * 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 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

View file

@ -31,7 +31,7 @@ public class MandelbrotSet {
private long startTime = System.currentTimeMillis(); private long startTime = System.currentTimeMillis();
private Controller controller; private final Controller controller;
/** /**
@ -163,15 +163,19 @@ public class MandelbrotSet {
if(finished){ if(finished){
System.out.println("CALCULATION FINISHED"); System.out.println("CALCULATION FINISHED");
synchronized (controller) {
controller.printOutput("CALCULATION FINISHED"); 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 // 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 endTime = System.currentTimeMillis();
long completionTimeLong = endTime - startTime; long completionTimeLong = endTime - startTime;
double completionTimeSec = (double) completionTimeLong / 1000.0; double completionTimeSec = (double) completionTimeLong / 1000.0;
System.out.println("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); System.out.println("Calculated " + frames + " frame/s in " + completionTimeSec + "s");
synchronized (controller) {
controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s"); controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s");
} }
} }
}
//SETTER //SETTER
public void setZoom(double zoom) { public void setZoom(double zoom) {