簡體   English   中英

如何在 Java 8 中並行處理更多文件處理

[英]How to handle more Files Handling in Java 8 in parallel

在我的 java web 應用程序是基於文件的集成。 他們曾經在我們的生產服務器 opt/app/proceed/ 文件夾中發送一堆 xml 文件(例如:10000)。 但是根據當前配置,我們的應用程序能夠在順序處理中處理 200 個文件。 因此,延遲處理文件。 我正在嘗試以並行方式增加文件處理的數量。 請找到代碼塊供您參考。

public class FileEx {

   public static void main(String[] args) throws IOException {
       String fileDir = "C:\\Users\\inputfiles"; //contains more than 10000 files
       new FileEx().traverseFilesFromDir(new File(fileDir));
   }

   public void traverseFilesFromDir(File dir) throws IOException {
       List<File> files = new ArrayList<File>();
       if (dir == null || !dir.isDirectory()) {
           throw new IllegalArgumentException("Not a valid directory (value: " + dir + ").");
       }
       File[] acknFiles = dir.listFiles();
       int fileCount = (acknFiles == null ? 0 : acknFiles.length);

       System.out.println("fileCount:::::::::" + fileCount);

       Arrays.sort(acknFiles, new Comparator<File>() {
           public int compare(File f1, File f2) {
               return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
           }
       });

       **int maxNoFiles = acknFiles.length <= 500 ? acknFiles.length : 500;**
       System.out.println(acknFiles.length + " Ackn found and starting to process oldest " + maxNoFiles + " files.");

       for (int i = 0; i < maxNoFiles; i++) {
           files.add(acknFiles[i]);
       }

       int fileCount1 = (files == null ? 0 : files.size());

       if (fileCount1 > 0) {
           for (int i = 0; i < fileCount1; i++) {

               boolean success = true;// processFile(files.get(i));
               if (success) {
                   System.out.println("File Successfully processed.");
               }
           }
       }
   }
}

如何着手改變文件處理方式。 等待需要的支持/指導。

// java 8.1 以上 並行 stream 你可以使用 // 這里 paralell() 默認使用 ForkJoinPool.commonPool() 執行

Files.lines(Paths.get( files.get(i))
  .parallel()
  .map(Your_FileBean::new) 
  .forEach(/*process Your_FileBean*/);

您可以通過Files.walk()執行此操作,它返回Stream<Path> object。 如果您想並行處理此 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ ,您可以嘗試使用parallel()collect(Collectors.toList()).parallelStream() 因為Files.walk()延遲評估, parallel()單獨可能無法有效利用所有可用內核。

在此之上,您可以根據需要應用排序過濾 您的處理步驟可以通過 stream 末尾的forEachOrdered()來實現。

這是一個例子:

Files.walk(Paths.get("/path/to/root/directory")) // create a stream of paths
    .collect(Collectors.toList()) // collect paths into list to better parallize
    .parallelStream() // process this stream in multiple threads
    .filter(Files::isRegularFile) // filter out any non-files (such as directories)
    .map(Path::toFile) // convert Path to File object
    .sorted((a, b) -> Long.compare(a.lastModified(), b.lastModified())) // sort files date
    .limit(500) // limit processing to 500 files (optional)
    .forEachOrdered(f -> {
        // do processing here
        System.out.println(f);
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM