Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ANTLR parsers for Java thread safe?

Is an ANTLR (v3.2) generated Java parser thread safe?

For example, in a servlet request handler, can I reuse the same parser instance to parse a request body? The requests may come in on different threads, so parsing must be done in a thread safe manner. if instances are thread safe, I can reuse the same instance in each request; else I must pool them or create new instances. The ANTLRv3 FAQ is silent on thread safety.

There is an ancient (2000) jGuru question about this, and the answer there,

Typically this question is really asking, "can I make multiple instances of the same parser and parse multiple input streams at the same time?"

makes the wrong assumption.

Since it is not mentioned in the ANTLR FAQ, I assume parsers are not thread safe.

like image 941
djb Avatar asked Oct 11 '13 19:10

djb


1 Answers

In ANTLR 3, there are no guarantees regarding thread safety. In ANTLR 4, instance methods are not safe for multi-threaded operations, but you may create two separate instances of the parser and use those separate instances on multiple threads.

like image 93
Sam Harwell Avatar answered Nov 15 '22 10:11

Sam Harwell