有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将java元素添加到多个线程中的列表时,会跳过这些元素

我正在处理一些图像,并将它们复制到多个线程中的某个位置。作为处理的一部分,我还将它们添加到列表中

列表(我在其中添加图像路径)缺少太多元素。我处理了大约2000张图片,列表中只有3张

以下是我的一些相关代码片段:

图像助手。爪哇

public class ImageHelper {

    private static List<String> filePaths = Collections.synchronizedList(new ArrayList<String>());

    public synchronized static List<String> getFilePaths(){
        return filePaths;
    }
}

ImageProcessRunnable。爪哇

public class ImageProcessRunnable implements Runnable {

    FObject action;
    ImageHelper imageHelper = new ImageHelper();

    public ImageProcessRunnable(FObject action) throws IOException {
        this.action = action;
    }

    public void run() {
        try {   
                    String filePath = imageProcessor.processImage(file, materialNumberImgName,
                            ImageHelper.getTempLocation(), ImageHelper.getBoxLocation(), idA2A2, logReport);
                    // I do get all the file paths in the above line from image processor.
                    ImageHelper.getFilePaths().add(filePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

图像服务。爪哇

public class ImageService {

List<FObject> list = new Vector<FObject>(results);

public static void loadImages() throws IOException {
        try {           
            SessionContext session = SessionContext.getContext(); 
            ExecutorService pool = Executors.newFixedThreadPool(numberOfThreads);

            for (FObject action : list) {
                if (action instanceof FObject) {    
                    Runnable run = new ImageProcessRunnable(action);
                    SessionThread th=new SessionThread(run, session);
                    pool.execute(th);
                }
                System.out.println("List Size is: "+ImageHelper.getFilePaths().size());
            }
            pool.shutdown();
            if (pool.awaitTermination(2, TimeUnit.SECONDS)) {
                pool.shutdownNow();
            }
        }
    }
}

代码正在处理所有图像并将其复制到驱动器,但它只是没有将所有图像添加到文件路径列表中


共 (0) 个答案