目前,我有一个解析器的设置,将通过约2万条记录的CSV文件解析。然后我应用一些过滤算法来清除要包含/排除的记录。最后将所有内容写回新的csv文件。Java解析器使用多线程
我已经做了一些基准测试,它原来是写数据到CSV是非常昂贵和过滤,并在同一时间附加到文件时造成巨大的减速。我想知道是否可以执行我所有的过滤,将行写入队列中,然后让第二个进程在队列已满或所有过滤完成时自行执行所有写入。
所以基本上总结:
Read line
Decide whether to discard or keep
if I'm keeping the file, add it to the "Write Queue"
Check if the write queue is full, if so, start the new process that will begin writing
Continue filtering until completed
感谢您的帮助!
编辑: 的方式我写如下:
FileWriter fw = new FileWriter("myFile.csv");
BufferedWriter bw = new BufferedWriter(fw);
while(read file...) {
//perform filters etc...
try {
bw.write(data.trim());
bw.newLine();
}catch(IOException e) {
System.out.println(e.getMessage());
}
你介意张贴你如何写出CSV文件的一些代码? – Shawn 2012-07-17 03:28:47
我用Python做这个确切的方法,并在完全不同的线程中读,写和处理。这是可能的。 – Blender 2012-07-17 03:30:26
@Blender你的Python解析器执行的效率是多少? – 1337holiday 2012-07-17 03:47:53