title: How to multi-thread an ANTLR parser in java
body: <p>I have my program that is proving slow in reading a file and then parsing it with antlr grammar. To improve performance of this I would like to multi-thread the parsing?</p>
  
  <p>Read File:</p>
  
  <pre><code>    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
  </code></pre>
  
  <p>The code parses the line with <code>pa.parse(inputLine);</code> which sends the input line in to an <code>ANTLRStringStream</code> and then a <code>CharStream</code> and is then parsed.
  Next <code>logrow=new LogData(pa,a);</code> gets the decoded values which will be storred in my table.</p>
  
  <p>My profiling shows the hotspots being in the lexer and parser classes which are built from my grammar (i.e. LogGrammarLexer and LogGrammarParser).
  Hope it's clear enough...</p>
  
  <p>Parsing:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>Also I have been reading up and now know that multi-threading file I/O is useless...
  Anyway it is the performances inside the built classes for my grammar doing complex decoding/parsing of each line in my file what I need to improve.</p>
  
  <p>So my problem is how to make it multi-threaded, </p>
  
  <p>Cheers</p>