In my program I have a while loop which iterates through an array list of a bunch of pictures and does a bunch of processing on them to change how they look and their image type and then writes them to the disk. My question is would adding multiple threads to process images or save the images speed things up, if so what would be the best way to go about it.
ArrayList images = new ArrayList ();//over 500 images
ArrayList paths = new ArrayList ();
int len = images.size();
for (int i =0; i < len ; i ++)
{
BufferedImage image = process (images.get(i))//takes about a second
ImageIO.write(image, "jpg", new File(getImagePaths().get(i)));
}
You could partition your list into n blocks so that you have blocks of size of list / n, and you could then have n threads operate on these “blocks” of images. This way you have more work that can be done concurrently.
To address some things brought up, it would most likely increase I/O concurrency as well because in a single threaded run it would fault on the first miss and block, then fault again to read the 2nd image, etc… In the multi threaded way it would block for n images at a time which allows the I/O scheduler to handle more I/O at a time (which is generally a good thing). This would mean increased performance even in single core processors due to the overlapping of I/O and the availability of more threads to run on the core while the blocked for I/O threads wait.