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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With