目前,我有一個解析器的設置,將通過約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