Multi-threading Java

I am trying to implement multi-threading in my Java Mandelbrot application: This is what I have so far: import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; public class MandelbrotSet { private int numberOfIterations; private double realMin; private double realMax; private double imaginaryMin; private double imaginaryMax; private int width; private int height; public BufferedImage image; public Graphics2D imageGraphics; public MandelbrotSet() { // Set the width and the height this.width = 600; this.height = 400; image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); imageGraphics = image.createGraphics(); this.realMin = -2.0; this.realMax = 1; this.imaginaryMin = -1; this.imaginaryMax = 1; this.numberOfIterations = 1000; } public Complex calculateComplexNumber(int x, int y) { double realPart = realMin + x * (realMax - realMin) / (this.getWidth() - 1); double imaginaryPart = imaginaryMax - y * (imaginaryMax - imaginaryMin) / (this.getHeight() - 1); return new Complex(realPart, imaginaryPart); } public void calculateMandelbrotImagePoints() { Thread[] threads = new Thread[4]; for (int i = 0; i < maxThreads; i++) { threads[i] = new Thread(new MThread(i)); threads[i].start(); } } class MThread implements Runnable { private int i; public MThread(int i) { this.i = i; } //Method uses the thread number to draw the mandelbrot in columns public void run() { for (int x = i; x < width; x += 4) { for (int y = 0; y < height; y++) { int n = 0; Complex c = calculateComplexNumber(x, y); Complex z = c; while ((zNumber.modulusSquared() < 4.0D) && (n < numberOfIterations)) { z = z.square(); z.add(c); n++; } if (n == numberOfIterations) { imageGraphics.setColor(Color.BLACK); } else { imageGraphics.setColor(Color.getHSBColor(n / 100.0F, 1, 1)); } imageGraphics.drawLine(x,y,x,y); } } } } } The problem that is occurring is that when the image is drawn incorrect pixels are displayed in the image: ![http://i.stack.imgur.com/wq2TN.png][1] When I check a thread with something like: threads[i].isAlive(); the image seems to display successfully, however the image takes much longer (up to 3x as long) to render. Two things I was wondering. 1. Where am I going wrong? 2. What would be the best way for drawing Mandelbrots to a `BufferedImage` for a large number of iterations (>1000)? [1]: http://i.stack.imgur.com/mXdYp.png

以上就是Multi-threading Java的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏