mirror of
https://github.com/Noratrieb/mandelbrotGUI.git
synced 2026-01-14 15:25:07 +01:00
fixed some multithreading bugs
This commit is contained in:
parent
580e3826b5
commit
5673c99d4e
2 changed files with 33 additions and 14 deletions
|
|
@ -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);
|
||||||
|
|
||||||
set.frameFinished(frameCounter);
|
synchronized (set) {
|
||||||
|
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");
|
||||||
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;
|
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");
|
|
||||||
|
|
||||||
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
|
* 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,9 +145,10 @@ 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
|
||||||
* @return -1 if the number is in the set, else it returns the amount of interations before it reached the threshold
|
* @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) {
|
int checkMandelbrot(CNumber number, int iterations, double threshold) {
|
||||||
|
|
|
||||||
|
|
@ -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,13 +163,17 @@ public class MandelbrotSet {
|
||||||
|
|
||||||
if(finished){
|
if(finished){
|
||||||
System.out.println("CALCULATION 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
|
// 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");
|
||||||
controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s");
|
synchronized (controller) {
|
||||||
|
controller.printOutput("Calculated " + frames + " frame/s in " + completionTimeSec + "s");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue