磁盘读取操作执行得很慢Java流

时间:2018-10-16 18:32:53

标签: java performance java-8 java-stream filestream

我需要从文件夹中读取图像并为其生成校验和。大约有330760张图像。以下是代码:

package com.test;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import org.apache.commons.codec.digest.DigestUtils;

public class FileTest2 {

    private void readFiles() throws IOException {
        try (Stream<Path> filePathStream = Files
                .walk(Paths.get("d:\\codebase\\images"))) {
            filePathStream.parallel().forEach(filePath -> {
                String checksumSHA256 = "";
                try {
                    checksumSHA256 = DigestUtils.sha384Hex(new FileInputStream(filePath.toString()));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if (Files.isRegularFile(filePath)) {
                    System.out.println(checksumSHA256);
                    System.out.println(filePath);
                    System.out.println("\n");

                }
            });
        }
    }

    public static void main(String[] args) throws IOException {
        long startTime = System.currentTimeMillis();
        FileTest2 fileTest = new FileTest2();
        fileTest.readFiles();
        long endTime = System.currentTimeMillis();
        System.out.println("Total Time took: " + (endTime - startTime) / 1000);
    }
}

花了大约36分钟。

系统配置:

核心数:8
内存:32 GB(15-17 GB可用)。其余的内存正在由另一台服务器使用。

36分钟太多了。有没有办法提高性能?

1 个答案:

答案 0 :(得分:2)

正如其他人指出的那样,您不终止执行程序。要查看实际时间,请运行以下

public static void main(String[] args) throws Exception {
    long startTime = System.currentTimeMillis();

    FileTest fileTest = new FileTest();
    fileTest.readFiles();

    long endTime = System.currentTimeMillis();
    System.out.println("Total Time took: "+ (endTime-startTime)/1000);
}

注意:至少从您发布的代码中,没有理由使用ExecutorService