java - 多线程Java

标签 java multithreading

我正在尝试在我的 Java Mandelbrot 应用程序中实现多线程:

这是我目前所拥有的:

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);
            }
         }
      }
   }
}

出现的问题是在绘制图像时图像中显示的像素不正确:

http://i.stack.imgur.com/wq2TN.png

当我用类似的东西检查线程时:

threads[i].isAlive();

图像似乎显示成功,但图像需要更长的时间(最多 3 倍)来渲染。

我想知道两件事。

  1. 我哪里出错了?

  2. 对于大量迭代 (>1000),将 Mandelbrots 绘制到 BufferedImage 的最佳方法是什么?

最佳答案

绘图不是线程安全的,因此不可能从多个线程绘制到同一个{屏幕、图像等}。您的线程可能会在这些行之间被中断(即可能发生上下文切换):

                  imageGraphics.setColor(Color.getHSBColor(n / 100.0F, 1, 1));
               }
               imageGraphics.drawLine(x,y,x,y);

一种选择是为每个线程提供自己的图像(例如,图像的四分之一作为图 block )进行绘制,然后在最后将这些图像绘制在一起。

关于java - 多线程Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9660429/

相关文章:

python - 多处理子流程

java - 无法找到java.lang.ArrayIndexOutOfBoundsException的原因

java - 在 Java 中对图像应用描边

java - 如何使用 v3 YouTube API 将视频设置为 "private yet shared"?

java - 如何从EditText中获取文本?

c - 在乒乓测试中使用 pthread 条件变量

java - 线程不执行

python - python中嵌入式循环的多线程

java - Tomcat ServletContext实现的getAttribute()方法可以不同步调用吗?

java - 一年中的编程作业月份