Added ThreadedJuliaSetCalculator
This commit is contained in:
parent
90c10083e9
commit
8b506b6433
4 changed files with 84 additions and 6 deletions
|
@ -55,7 +55,10 @@ public class ForkJoinJuliaSetCalculator implements JuliaSetCalculator {
|
|||
int middle = (startInclusive + endExclusive) / 2;
|
||||
JuliaTask task1 = new JuliaTask(startInclusive, middle);
|
||||
JuliaTask task2 = new JuliaTask(middle, endExclusive);
|
||||
invokeAll(task1, task2);
|
||||
//invokeAll(task1, task2);
|
||||
task1.fork();
|
||||
task2.compute();
|
||||
task1.join(); // Interestingly, if I comment out this join, it seems to work ok???
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +73,7 @@ public class ForkJoinJuliaSetCalculator implements JuliaSetCalculator {
|
|||
System.out.println(endTime - startTime);
|
||||
|
||||
return iterations;
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ public class JuliaSetPanel extends JPanel {
|
|||
|
||||
public JuliaSetPanel(final JuliaSetCalculator juliaSetCalculator, final int width, final int height) {
|
||||
this.juliaSetCalculator = juliaSetCalculator;
|
||||
setPreferredSize(new Dimension(1200, 800));
|
||||
setPreferredSize(new Dimension(width, height));
|
||||
setBackground(Color.white);
|
||||
}
|
||||
|
||||
|
|
|
@ -5,9 +5,9 @@ public class MainClass {
|
|||
|
||||
public static void main(String[] args) {
|
||||
|
||||
int width = 1200;
|
||||
int height = 800;
|
||||
JuliaSetCalculator juliaSetCalculator = new ForkJoinJuliaSetCalculator(width, height);
|
||||
int width = 1600;
|
||||
int height = 1000;
|
||||
JuliaSetCalculator juliaSetCalculator = new ThreadedJuliaSetCalculator(width, height);
|
||||
JuliaSetPanel juliaSetPanel = new JuliaSetPanel(juliaSetCalculator, width, height);
|
||||
|
||||
SwingUtilities.invokeLater(() -> {
|
||||
|
|
75
src/ThreadedJuliaSetCalculator.java
Normal file
75
src/ThreadedJuliaSetCalculator.java
Normal file
|
@ -0,0 +1,75 @@
|
|||
public class ThreadedJuliaSetCalculator implements JuliaSetCalculator {
|
||||
|
||||
private int width;
|
||||
private int height;
|
||||
private int[][] iterations;
|
||||
|
||||
public ThreadedJuliaSetCalculator(final int width, final int height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
iterations = new int[width][height];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int[][] calculate(int maxIterations, double zoom, double cx, double cy, double moveX, double moveY) {
|
||||
|
||||
class JuliaTask implements Runnable {
|
||||
|
||||
private int startInclusive;
|
||||
private int endExclusive;
|
||||
|
||||
JuliaTask(int startInclusive, int endExclusive) {
|
||||
this.startInclusive = startInclusive;
|
||||
this.endExclusive = endExclusive;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
double zx, zy;
|
||||
|
||||
for(int x = startInclusive; x < endExclusive; x++) {
|
||||
for (int y = 0; y < height; y++) {
|
||||
zx = 1.5 * (x - width / 2) / (0.5 * zoom * width) + moveX;
|
||||
zy = (y - height / 2) / (0.5 * zoom * height) + moveY;
|
||||
int i = maxIterations;
|
||||
while (zx * zx + zy * zy < 4 && i > 0) {
|
||||
double tmp = zx * zx - zy * zy + cx;
|
||||
zy = 2.0 * zx * zy + cy;
|
||||
zx = tmp;
|
||||
i--;
|
||||
}
|
||||
iterations[x][y] = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
long startTime = System.currentTimeMillis();
|
||||
|
||||
int NUM_THREADS = 128;
|
||||
Thread[] threads = new Thread[NUM_THREADS];
|
||||
int step = width / NUM_THREADS;
|
||||
|
||||
for(int i = 0; i < NUM_THREADS; i++) {
|
||||
int start = i * step;
|
||||
int end = (i + 1) * step;
|
||||
if(end > width || i == NUM_THREADS - 1) end = width;
|
||||
threads[i] = new Thread(new JuliaTask(start, end));
|
||||
threads[i].start();
|
||||
}
|
||||
|
||||
for(int i = 0; i < NUM_THREADS; i++) {
|
||||
try {
|
||||
threads[i].join();
|
||||
} catch(Exception e) {
|
||||
// No!
|
||||
}
|
||||
}
|
||||
|
||||
long endTime = System.currentTimeMillis();
|
||||
System.out.println(endTime - startTime);
|
||||
|
||||
return iterations;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue