我在通过 ForkJoinPool 加载大量图像时遇到问题,我正在使用超线程的 4 核 Intel 进行测试,因此有 8 个逻辑线程。但是,我将池限制为只有 4 个线程。我收到来自 ImageIO 的错误,无法找到图像。
public class LoadImages extends RecursiveAction {
private static final long serialVersionUID = 1L;
//this is an example
private static int threadThreshold = totalImages/totalThreads + 2;
private String[] imgArr;
private int arrStart = 0;
private int arrSize = 0;
public LoadImages(String[] imgs, int start, int size) {
imgArr = imgs;
arrSize = size;
arrStart = start;
}
protected void processImages(){
BufferedImage img = null;
for (int i = arrStart; i < arrStart + arrSize; i++) {
try{
img = ImageIO.read(new File(imgArr[i]));
} catch (IOException | CMMException | NullPointerException e) {
System.out.println(imgArr[i]);
e.printStackTrace();
img = null;
}
...
}
}
protected void compute() {
// Check the number of files
if (arrSize <= threadThreshold) {
processImages();
return;
} else {
int split = arrSize / 2;
invokeAll(new LoadImages(imgArr, arrStart, split), new LoadImages(imgArr, arrStart + split, arrSize - split));
}
}
}
任何关于我做错了什么的见解都会很棒,我注意到它真的只有在我有超过 1700 张图像并且所有图像都超过 5MB 时才会中断。
这是我从 Java 收到的错误:
javax.imageio.IIOException: Can't create an ImageInputStream!
at javax.imageio.ImageIO.read(Unknown Source)
当我知道文件在那里时。我将此代码用作指南: https ://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html