我的程序在读取文件然后用 antlr 语法解析它时被证明很慢。为了提高性能,我想多线程解析?
阅读文件:
LogParser pa = new LogParser();
LogData logrow;
String inputLine;
int a=0;
try {
//feed line by line
FileReader fr = new FileReader(jFileChooser1.getSelectedFile());
BufferedReader reader = new BufferedReader(fr);
while ((inputLine = reader.readLine()) != null)
{
try {
a++;
jProgressBar.setValue(a);
pa.parse(inputLine); //decode the line
} catch ... catches errors and send to logger
} finally {
logrow=new LogData(pa,a);
mLogTable.addRow(logrow);//store the decoded line
}
}
reader.close();
} catch ... catches errors and send to logger
该代码解析pa.parse(inputLine);
将输入行发送到 anANTLRStringStream
然后是 a 的行,然后CharStream
再进行解析。接下来logrow=new LogData(pa,a);
获取将存储在我的表中的解码值。
我的分析显示热点位于根据我的语法构建的词法分析器和解析器类中(即 LogGrammarLexer 和 LogGrammarParser)。希望它足够清楚......
解析:
LogGrammarLexer lexer = new LogGrammarLexer(inStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
decoded = new LogGrammarParser(tokens);
try {
failurePosition="";
decoded.logLine();
} catch (RecognitionException e) {
failurePosition=Integer.toString(e.charPositionInLine);
} catch (Exception e) {
failurePosition="-3";
throw e;
} finally {
return decoded;//TODO: see if return is necessary as decoded is now a field in class
}
此外,我一直在阅读,现在知道多线程文件 I/O 是无用的......无论如何,这是我的语法在构建类中的性能,对我的文件中的每一行进行复杂的解码/解析,我需要改进.
所以我的问题是如何使它成为多线程的,
干杯