Java中使用4个线程的Mandelbrot
如果我试图在Mandelbrot程序中实现4个线程,有人能告诉我这段代码有什么问题吗。文件中图像的前半部分未渲染。谢谢Java中使用4个线程的Mandelbrot,java,multithreading,Java,Multithreading,如果我试图在Mandelbrot程序中实现4个线程,有人能告诉我这段代码有什么问题吗。文件中图像的前半部分未渲染。谢谢 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Week2;
/**
*
* @author Alex Humphris
*/
import java.awt.Color;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public class ParallelMandelbrot4 extends Thread {
final static int N = 4096;
final static int CUTOFF = 100;
static int[][] set = new int[N][N];
public static void main(String[] args) throws Exception {
// Calculate set
long startTime = System.currentTimeMillis();
ParallelMandelbrot4 thread0 = new ParallelMandelbrot4(0);
ParallelMandelbrot4 thread1 = new ParallelMandelbrot4(1);
ParallelMandelbrot4 thread2 = new ParallelMandelbrot4(2);
ParallelMandelbrot4 thread3 = new ParallelMandelbrot4(3);
thread0.start();
thread1.start();
thread2.start();
thread3.start();
thread0.join();
thread1.join();
thread2.join();
thread3.join();
long endTime = System.currentTimeMillis();
System.out.println("Calculation completed in "
+ (endTime - startTime) + " milliseconds");
// Plot image
BufferedImage img = new BufferedImage(N, N,
BufferedImage.TYPE_INT_ARGB);
// Draw pixels
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int k = set[i][j];
float level;
if (k < CUTOFF) {
level = (float) k / CUTOFF;
} else {
level = 0;
}
Color c = new Color(0, level, 0); // Green
img.setRGB(i, j, c.getRGB());
}
}
// Print file
ImageIO.write(img, "PNG", new File("Mandelbrot.png"));
}
int me;
public ParallelMandelbrot4(int me) {
this.me = me;
}
public void run() {
int begin, end;
if (me == 0) {
begin = 0;
end = (N / 4) * 1;
}
if (me == 1) {
begin = (N / 4) * 1;
end = (N / 4) * 2;
}
if (me == 2) {
begin = (N / 4) * 2;
end = (N / 4) * 3;
} else { // me == 1
begin = (N / 4) * 3;
end = N;
}
for (int i = begin; i < end; i++) {
for (int j = 0; j < N; j++) {
double cr = (4.0 * i - 2 * N) / N;
double ci = (4.0 * j - 2 * N) / N;
double zr = cr, zi = ci;
int k = 0;
while (k < CUTOFF && zr * zr + zi * zi < 4.0) {
// z = c + z * z
double newr = cr + zr * zr - zi * zi;
double newi = ci + 2 * zr * zi;
zr = newr;
zi = newi;
k++;
}
set[i][j] = k;
}
}
}
}
/*
*要更改此许可证标题,请在“项目属性”中选择“许可证标题”。
*要更改此模板文件,请选择工具|模板
*然后在编辑器中打开模板。
*/
套餐周2;
/**
*
*@作者亚历克斯·汉弗莱斯
*/
导入java.awt.Color;
导入java.awt.image.buffereImage;
导入javax.imageio.imageio;
导入java.io.File;
公共类ParallelBrot4扩展线程{
最终静态积分N=4096;
最终静态整数截止=100;
静态int[][]集=新int[N][N];
公共静态void main(字符串[]args)引发异常{
//计算集
long startTime=System.currentTimeMillis();
ParallelMandelbrot4 thread0=新的ParallelMandelbrot4(0);
ParallelMandelbrot4螺纹1=新的ParallelMandelbrot4(1);
ParallelMandelbrot4螺纹2=新的ParallelMandelbrot4(2);
ParallelMandelbrot4螺纹3=新的ParallelMandelbrot4(3);
thread0.start();
thread1.start();
thread2.start();
thread3.start();
thread0.join();
thread1.join();
螺纹2.连接();
螺纹3.连接();
long-endTime=System.currentTimeMillis();
System.out.println(“在中完成计算”
+(endTime-startTime)+“毫秒”);
//绘图图像
BuffereImage img=新的BuffereImage(N,N,
BuffereImage.TYPE_INT_ARGB);
//绘制像素
对于(int i=0;i
run()语句中存在错误。通过为(me==3)
添加if语句,将呈现整个图像,而之前,末尾的else
语句是在3个不同的线程中调用的
这是因为代码末尾的else。比如说me
是1。当它为1时,它将执行if(me==1)
语句中的代码,并且它还将在末尾执行代码,因为me
不等于2
要解决这个问题,我建议使用if-else
语句:
int begin = 0, end = 0;
if (me == 0) {
begin = 0;
end = (N / 4) * 1;
}
else if (me == 1) {
begin = (N / 4) * 1;
end = (N / 4) * 2;
}
else if (me == 2) {
begin = (N / 4) * 2;
end = (N / 4) * 3;
}
else if (me == 3) {
begin = (N / 4) * 3;
end = N;
}
多线程部分几乎是正确的。是构造器出了一些问题。你应该考虑把<代码>我>代码>最终的私有变量。@斯凯普特的回答是正确的。这就是它在我的IDE中的外观,它让我对您正在使用的
if-else
的趣味性有了一个想法
因此,解决这个问题应该是可行的:
private final int me;
public ParallelMandelbrot4(int me) {
this.me = me;
}
public void run() {
int begin, end;
if (me == 0) {
begin = 0;
end = (N / 4) * 1;
}
else if (me == 1) {
begin = (N / 4) * 1;
end = (N / 4) * 2;
}
else if (me == 2) {
begin = (N / 4) * 2;
end = (N / 4) * 3;
} else { // me == 3
begin = (N / 4) * 3;
end = N;
}
非常感谢。我知道在我学习新概念的时候,这会是件愚蠢的事情。