2012-12-22 74 views
1

我正在尝试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)

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

+4

你试过用y而不是x分裂吗?我认为BufferedImage很可能在内存中存储行 - 主要,这可能会有所作为。 – yiding

+1

更大的问题可能是BufferedImage和Graphics对象不是线程安全的。您可能想要在数组中生成噪声,并直接从数据创建一个BufferedImage(这也将比使用setRGB更快)。 – yiding

+0

至于你的第一个评论:我已经转向Y分裂,但它没有区别。 – Paul

回答

2

首先,F/J是一个利基产品。如果你没有一个巨大的数组并将其作为DAG处理,那么你使用的是错误的产品。当然,F/J可以使用多个处理器,但是也可以使用简单的多线程方法,而不需要F/J的所有开销。

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

这是路F /Ĵ本来是用于:

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; 

当你不走结构树的叶子,那么所有的赌注都关闭。

+0

你可能是对的,但这个程序的重点是测试ForkJoin的运作(如上所述)。所以,这仍然让我想知道为什么这样发生? – Paul

+0

如何用fork(),join()替换inokeAll()这种递归分解框架的设计工作方式。 – edharned

0

由于开销?分叉和连接也需要时间。也许你需要一个更大的测试集?或者在线索本身做更多的工作?

+0

在四核上执行时间约为16秒,因此64sec核心时间。在6000 * 6000图像上有2M限制,这样就可以产生32个线程,即每个线程执行时间大约为2秒。所以创建线程的开销应该相当低,我认为呢?另一方面,单线程执行需要9,5秒的核心时间。但是那些55sec核心时间开销在哪里呢? – Paul