Оптимизация параллельной обработки многих файлов

У меня есть часть программы, обрабатывающая много файлов, где для каждого файла нужно сделать две вещи: сначала некоторую часть файла прочитать и обработать, а затем полученную в результатеMyFileData хранится Первая часть может быть распараллелена, вторая - нет.

Последовательное выполнение всего очень медленно, так как процессор должен ждать диск, затем он работает немного, а затем выдает другой запрос и снова ждет ...

Я сделал следующее

class MyCallable implements Callable<MyFileData> {
    MyCallable(File file) {
        this.file = file;
    }
    public MyFileData call() {
        return someSlowOperation(file);
    }
    private final File file;
}

for (File f : files) futures.add(executorService.submit(new MyCallable(f)));
for (Future<MyFileData> f : futures) sequentialOperation(f.get());

и это очень помогло. Однако мне бы хотелось улучшить две вещи:

The sequentialOperation gets executed in a fixed order instead of processing whatever result is available first. How can I change it?

There are thousands of files to be processed and starting thousands of disk requests could lead to disk trashing. By using Executors.newFixedThreadPool(10) I've limited this number, however I'm looking for something better. Ideally it should be self-tuning, so that it works optimal on different computers (e.g., issues more requests when RAID and/or NCQ is available, etc.). I don't think it could be based on finding out the HW configuration, but measuring the processing speed and optimizing based on it should somehow be possible. Any idea?

Ответы на вопрос(2)

Ваш ответ на вопрос