2016-02-10 37 views
1

我一直在玩一些多线程图像处理代码,读取图像并将其转换为灰度2种方式 - 依次,然后并行,所以我可以比较两者的区别。Java - 多线程在很长的时间很小的图像

我做的一件事是做一个绝对小图像,只有4 x 4px的一个纯色。顺序版本通常在大约20ms内运行,并且(4线程)并行版本有时会这样做,但有时它似乎会“卡住”并花费很长时间,有时甚至长达1.5秒。这似乎不会发生(?)少于4个线程,所以我只是想知道是什么原因导致它减慢了这么多?我有一些想法,主要是可能是为很小的图像设置多个线程的开销不值得,但1.5秒是需要等待的很长时间,比任何线程创建时都要多高架。

这里是源代码:

PixelsManipulation.java(主类):

public final class PixelsManipulation{ 

private static Sequential sequentialGrayscaler = new Sequential(); 

public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException { 

File file = new File("src/pixelsmanipulation/hiresimage.jpg"); 
FileInputStream fis = new FileInputStream(file); 
BufferedImage image = ImageIO.read(fis); //reading the image file 

int rows = 2; // 2 rows and 2 cols will split the image into quarters 
int cols = 2; 
int chunks = rows * cols; // 4 chunks, one for each quarter of the image 
int chunkWidth = image.getWidth()/cols; // determines the chunk width and height 
int chunkHeight = image.getHeight()/rows; 
int count = 0; 
BufferedImage imgs[] = new BufferedImage[chunks]; // Array to hold image chunks 

for (int x = 0; x < rows; x++) { 
    for (int y = 0; y < cols; y++) { 
     //Initialize the image array with image chunks 
     imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType()); 
     // draws the image chunk 

     Graphics2D gr = imgs[count++].createGraphics(); // Actually create an image for us to use 
     gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null); 
     gr.dispose(); 

    } 
} 

//writing mini images into image files 
for (int i = 0; i < imgs.length; i++) { 
    ImageIO.write(imgs[i], "jpg", new File("img" + i + ".jpg")); 
} 
System.out.println("Mini images created"); 

// Start threads with their respective quarters (chunks) of the image to work on 
// I have a quad-core machine, so I can only use 4 threads on my CPU 
Parallel parallelGrayscaler = new Parallel("thread-1", imgs[0]); 
Parallel parallelGrayscaler2 = new Parallel("thread-2", imgs[1]); 
Parallel parallelGrayscaler3 = new Parallel("thread-3", imgs[2]); 
Parallel parallelGrayscaler4 = new Parallel("thread-4", imgs[3]); 

// Sequential: 
long startTime = System.currentTimeMillis(); 

sequentialGrayscaler.ConvertToGrayscale(image); 

long stopTime = System.currentTimeMillis(); 
long elapsedTime = stopTime - startTime; 
System.out.println("Sequential code executed in " + elapsedTime + " ms."); 

// Multithreaded (parallel): 
startTime = System.currentTimeMillis(); 

parallelGrayscaler.start(); 
parallelGrayscaler2.start(); 
parallelGrayscaler3.start(); 
parallelGrayscaler4.start(); 

// Main waits for threads to finish so that the program doesn't "end" (i.e. stop measuring time) before the threads finish 
parallelGrayscaler.join(); 
parallelGrayscaler2.join(); 
parallelGrayscaler3.join(); 
parallelGrayscaler4.join(); 

stopTime = System.currentTimeMillis(); 
elapsedTime = stopTime - startTime; 
System.out.println("Multithreaded (parallel) code executed in " + elapsedTime + " ms."); 
} 
} 

Parallel.java:

// Let each of the 4 threads work on a different quarter of the image 
public class Parallel extends Thread{//implements Runnable{ 

private String threadName; 
private BufferedImage myImage; // Calling it "my" image because each thread will have its own unique quarter of the image to work on 
private int width, height; // Image params 

Parallel(String name, BufferedImage image){ 
threadName = name; 
System.out.println("Creating "+ threadName); 
myImage = image; 
width = myImage.getWidth(); 
height = myImage.getHeight(); 

} 

public void run(){ 
System.out.println("Running " + threadName); 

// Pixel by pixel (for our quarter of the image) 
for (int j = 0; j < height; j++){ 
    for (int i = 0; i < width; i++){ 

     // Traversing the image and converting the RGB values (doing the same thing as the sequential code but on a smaller scale) 
     Color c = new Color(myImage.getRGB(i,j)); 

     int red = (int)(c.getRed() * 0.299); 
     int green = (int)(c.getGreen() * 0.587); 
     int blue = (int)(c.getBlue() * 0.114); 

     Color newColor = new Color(red + green + blue, red + green + blue, red + green + blue); 

     myImage.setRGB(i,j,newColor.getRGB()); // Write the new value for that pixel 


    } 
} 

File output = new File("src/pixelsmanipulation/"+threadName+"grayscale.jpg"); // Put it in a "lower level" folder so we can see it in the project view 
try { 
    ImageIO.write(newImage, "jpg", output); 
} catch (IOException ex) { 
    Logger.getLogger(Parallel.class.getName()).log(Level.SEVERE, null, ex); 
} 
System.out.println("Thread " + threadName + " exiting. ---"); 
} 
} 

编辑:这里是从执行的示例的日志:

Creating thread-1 
Creating thread-2 
Creating thread-3 
Creating thread-4 
Sequential code executed in 5 ms. 
Running thread-2 
Running thread-1 
Running thread-3 
Thread thread-1 exiting. --- 
Thread thread-2 exiting. --- 
Thread thread-3 exiting. --- 
Running thread-4 
Thread thread-4 exiting. --- 
Multithreaded (parallel) code executed in 5 ms. 

奇怪,我似乎无法复制延迟,我现在在不同的机器上,我原来的工作。以某种方式处理器的差异(都是四核)?我会尝试从原始机器获取日志。

编辑2:正如Gee Bee所说,这很可能是由于慢速度似乎只发生在HDD而不是SSD上的事实的组合,这是由于我正在写入文件线程,并且这通常在HDD上较慢。取出文件编写代码会使线程运行得更快,并且只需在SSD上运行即可(尽管我认为写入线程内的文件并非真正最佳,应该避免)。

+0

尝试取出文件写入,看看它是如何影响你的结果。这很可能是每个线程花费最多的时间。另外,您可以添加更多的日志语句(带时间戳)来查看延迟的位置。 – pcarter

+0

不幸的是,我似乎无法复制延迟,现在我在不同的机器上,比我最初。这可能是处理器之间的差异(都是四核)吗?我会尝试从原始机器获取日志。 – Touchdown

回答

1

这个问题相当棘手,1.5秒很可能涉及锁定问题。

  • 顺序:

    运行代码后150毫秒

  • 并行:57毫秒(2×2,4个线程)

现在每个处理线程做了很多事情:

  • 在像素级访问图像(由于各种原因,这是非常耗费资源的操作)
  • 写入文件
  • 进行JPEG压缩

我建议从实际处理的文件写入加上JPEG编码隔离和恢复您的测量。

如果你有4个线程,现在你遇到4次JPEG编码,4次并行文件写入,这可能会产生问题。我使用的是SSD,所以文件写入没有任何区别,但在HDD上可以产生影响。

请注意,使用比物理内核更多的线程不会使并行操作更快,但只会增加额外的开销。 另请注意,如果图片太小,“平行”线程不能并行工作。 Are parallel drawing operations possible with Java Graphics2d?,因为你正在使用四种不同的bufferedimages从四个不同的线程,这并不影响你的表现:相反,而你只是tarting线3

虽然AWT强加一个BufferedImage锁定的第一个线程已经完成。

所以,你的想法将工作。但是如果计算速度很快,那么4个线程的性能改进就太小了。尽量不要测量你无法控制的操作(例如,根据你的硬件和当前的虚拟内存条件,文件IO性能可以是任何东西)。

+0

HDD和SSD之间的差异并不是我认为的 - 我现在在SSD上,但原来的机器有一个硬盘,这可能会导致一些“看不见的”问题。我会把这个文件写出来,并做一些其他的事情,看看我能够加快速度。 – Touchdown