【发布时间】: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