【问题标题】:Java ForkJoin Multi-threaded is slower than single threadJava ForkJoin 多线程比单线程慢
【发布时间】:2012-12-22 03:01:28
【问题描述】:

我正在尝试 Java ForkJoin 框架并编写了一个简单的测试程序,将图像的像素设置为随机颜色。例如。它会产生伪噪声。

但在测试性能时,我发现运行单线程实际上比使用多线程运行更快。我通过一个高阈值让它运行单线程。

这是类工作者类:

public class Noise extends RecursiveAction {

    private BufferedImage image;
    private int xMin;
    private int yMin;
    private int xMax;
    private int yMax;
    private int threshold = 2000000; // max pixels per thread

    public Noise(BufferedImage image, int xMin, int yMin, int xMax, int yMax, int threshold) {
        this.image = image;
        this.xMin = xMin;
        this.yMin = yMin;
        this.xMax = xMax;
        this.yMax = yMax;
        this.threshold = threshold;
    }

    public Noise(BufferedImage image, int xMin, int yMin, int xMax, int yMax) {
        this.image = image;
        this.xMin = xMin;
        this.yMin = yMin;
        this.xMax = xMax;
        this.yMax = yMax;
    }

    @Override
    protected void compute() {
        int ppt = (xMax - xMin) * (yMax - yMin); // pixels pet thread
        if(ppt > threshold) {
            // split
            int verdeling = ((xMax - xMin) / 2) + xMin;
            invokeAll(new Noise(image, xMin, yMin, verdeling, yMax),
                    new Noise(image, verdeling+1, yMin, xMax, yMax));
        }
        else {
            // execute!
            computeDirectly(xMin, yMin, xMax, yMax);
        }
    }

    private void computeDirectly(int xMin, int yMin, int xMax, int yMax) {
        Random generator = new Random();
        for (int x = xMin; x < xMax; x++) {
            for (int y = yMin; y < yMax; y++) {
                //image.setPaint(new Color(generator.nextInt()));
                int rgb = generator.nextInt();
                int red = (rgb >> 16) & 0xFF;
                int green = (rgb >> 8) & 0xFF;
                int blue = rgb & 0xFF;

                red = (int) Math.round((Math.log(255L) / Math.log((double) red)) * 255);
                green = (int) Math.round((Math.log(255L) / Math.log((double) green)) * 255);
                blue = (int) Math.round((Math.log(255L) / Math.log((double) blue)) * 255);

                int rgbSat = red;
                rgbSat = (rgbSat << 8) + green;
                rgbSat = (rgbSat << 8) + blue;

                image.setRGB(x, y, rgbSat);
            }

        }
        Graphics2D g2D = image.createGraphics();
        g2D.setPaint(Color.RED);
        g2D.drawRect(xMin, yMin, xMax-xMin, yMax-yMin);
    }
}

生成 6000 * 6000 的图像时,结果是:
单线程:9.4 秒 @ 25% CPU 负载
多线程:16.5 秒 @ 80%-90% CPU 负载
(Core2quad Q9450)

为什么多线程版本比较慢?
我该如何解决这个问题?

【问题讨论】:

  • 你试过用 y 而不是 x 分割吗?我认为 BufferedImage 可能会以行为主存储在内存中,这可能会有所不同。
  • 更大的问题可能是 BufferedImage 和 Graphics 对象不是线程安全的。您可能希望在数组中生成噪声,并在最后直接从数据中创建一个 BufferedImage(这也将比使用 setRGB 更快)。
  • 至于您的第一条评论:我已切换到按 Y 拆分,但没有区别。
  • 如果你可以优化代码。例如删除日志。与使用多线程相比,您可能会获得更多的速度提升
  • 对于您在没有 fork/join 的情况下比较的单线程性能,因为它可能会更快,因为它会更简单。

标签: java multithreading fork-join


【解决方案1】:

首先,F/J 是小众产品。如果您没有 HUGE 数组并将其作为 DAG 处理,那么您使用的是错误的产品。当然,F/J 可以使用多个处理器,但也可以只使用简单的多线程方法,而无需 F/J 的所有开销。

尝试使用四个线程,然后直接给每个线程四分之一的工作。

这就是 F/J 的用途:

Sum left  = new Sum(array, low, mid);
Sum right = new Sum(array, mid, high);
left.fork();
long rightAns = right.compute();
long leftAns   = left.join();
return leftAns + rightAns;

如果你没有走下结构化树的叶子,那么所有的赌注都没有了。

【讨论】:

  • 你可能是对的,但这个程序的目的是测试 ForkJoin 的工作原理(如前所述)。所以这仍然让我想知道为什么会这样?
  • 如何用 fork()、join() 替换 inokeAll(),就像这个递归分解框架设计的工作方式一样。
【解决方案2】:

我认为您发现的问题与您的代码直接无关,罪魁祸首是BufferedImage#setRGB()方法:

public synchronized void setRGB(int x, int y, int rgb) {...}

所以测试用例很慢,因为这个方法的争用。

作为 JDK 8 中的一种解决方法,您可以使用类似的方法:

public void setRGB(int startX, int startY, int w, int h,
                   int[] rgbArray, int offset, int scansize) {...}

由于 JDK 10 这不再是问题,已删除同步关键字:https://bugs.openjdk.java.net/browse/JDK-8183576

这是我的执行时间:

JDK 8 JDK 17
single-thread 3 sec 3 sec
multi-thread 7 sec 1 sec

【讨论】:

    【解决方案3】:

    我认为您想使用 ThreadLocalRandom 而不是 Random。

    http://docs.oracle.com/javase/tutorial/essential/concurrency/threadlocalrandom.html

    【讨论】:

      【解决方案4】:

      因为开销?分叉和连接也需要时间。也许您需要更大的测试集?还是在线程本身中做更多的工作?

      【讨论】:

      • 四核上的执行时间约为 16 秒,因此核心时间为 64 秒。对于 6000*6000 的图像有 2M 的限制,因此产生 32 个线程,即每个线程大约 2 秒的执行时间。所以我认为创建线程的开销应该相当低?另一方面,单线程执行需要 9.5 秒的核心时间。但是那些 55 秒的核心时间开销去哪儿了?
      猜你喜欢
      • 2012-09-05
      • 1970-01-01
      • 2020-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 2011-03-01
      相关资源
      最近更新 更多