Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多文件的多线程处理_Java_Multithreading - Fatal编程技术网

Java 多文件的多线程处理

Java 多文件的多线程处理,java,multithreading,Java,Multithreading,我有一个java程序,可以执行以下算法: Given a directory. For each file in the directory. read each line process the line with regex and other string operation in addition to parsing write the line after processing to an output file 该目录大约有10个文件,大约

我有一个java程序,可以执行以下算法:

Given a directory.
For each file in the directory.
    read each line
        process the line with regex and other string operation in addition to parsing
        write the line after processing to an output file
该目录大约有10个文件,大约有300万行

由于正则表达式的复杂性,处理部分似乎是性能的瓶颈,而解析部分则更慢。 因为我有一台功能强大的机器,有大量的ram,而且正如“cat/proc/cpuinfo | grep processor | wc-l”所述,它包含16个CPU。如果不把这些事情牵扯进来,那将是令人伤心的

我的想法是首先将所有文件读入一个数组列表。然后将列表划分为与所需线程数相等的子数组列表。在运行线程之后,每个线程都要执行给定的子列表 同时处理部分,并将输出保存到新的子阵列中,该子阵列最后应合并并刷新到输出文件中

这是解决这个问题的正确方法吗

如能参考其他类似作品,将不胜感激

非常感谢

    String dir = "path/to/dir";
    File folder = new File(dir);
    if (folder.isDirectory()) {
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < listOfFiles.length; i++) {
            File file = listOfFiles[i];
            if (file.isFile() && file.getName().contains("log")) {
                System.out.println("processing file: " + file);
                test.readFile(file);
            }
        }
    }
}

PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8), true);
public void readFile(File file) {
    FileInputStream fis = null;
    BufferedReader br = null;
    String line;

    try {
        fis = new FileInputStream(file);
        br = new BufferedReader(new InputStreamReader(fis,
                Charset.forName("UTF-8")));
        while ((line = br.readLine()) != null) {
            String processedLine = processingLine(line);
            if (processedLine != null){
                pw.println(processedLine);
            }

        }

    } catch (IOException e) {

    }
}

public String processingLine(String line) {
    //regex
    //string operations
    //parsing text
}
String dir=“path/to/dir”;
文件夹=新文件(目录);
if(folder.isDirectory()){
File[]listOfFiles=folder.listFiles();
for(int i=0;i
  • 使用java.util.concurrency.Executors为每个文件处理生成子线程
  • 使用同步队列收集结果
  • 在单独的线程中处理收集的队列,将数据(安全且未损坏)写入输出

每个输入文件都有自己的输出文件吗?或者它们共享一个输出文件?让Java程序按顺序处理一个文件,并为每10个文件启动一个程序副本会简单得多。我很惊讶解析一行的速度如此之慢。正则表达式/解析代码是什么样子的?似乎每次调用该方法时(数百万次)都在重新创建相同的模式,而不是重用它。那是个坏主意。对于这样的代码,如果编码正确,我希望IO是限制因素。第一个提示:使用BufferedWriter。每行有多长?此外,这些文件是否存储在单个驱动器上?您可以为每个文件使用一个线程,读取每个文件,并将其内容添加到共享的、阻塞的队列中。然后,您可以使用另一系列线程从该队列读取并处理字符串,您可能需要将结果放入另一个队列,以便通过单个线程控制输出的写入。这假设顺序不重要。。。