我正在嘗試Java ForkJoin框架並編寫了一個簡單的測試程序,將圖像的像素設置爲隨機顏色。例如。它會產生僞噪聲。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.4sec @ 25%的CPU負載
多線程:16.5sec @ 80 %-90%CPU負載
(Core2quad Q9450)
爲什麼多線程版本較慢?
我該如何解決這個問題?
你試過用y而不是x分裂嗎?我認爲BufferedImage很可能在內存中存儲行 - 主要,這可能會有所作爲。 – yiding
更大的問題可能是BufferedImage和Graphics對象不是線程安全的。您可能想要在數組中生成噪聲,並直接從數據創建一個BufferedImage(這也將比使用setRGB更快)。 – yiding
至於你的第一個評論:我已經轉向Y分裂,但它沒有區別。 – Paul