Java Thread safety again… this time Regexes?

Over at there was a Java brain teaser…

Suppose that you have a Java web application where regular expressions are used deep down in the implementation to do some work, but you observe that the an array index exception is occurring sporadically where the regular expressions are being used.

What’s causing the exceptions? What’s the solution?

Just a guess on my part at 23:10 in the evening…


java.util.regex.Matcher: Instances of this class are not safe for use by multiple concurrent threads.

Or using Jakarta….

However, RE and RECompiler are not threadsafe (for efficiency reasons…)


Swap out your pre-compiled and “more efficient” Matcher object for this less performant version:

static Pattern pat = Pattern.compile(regexString);
// return a matcher to a waiting thread... or do the matching all in here... 
// or do more locking which I'm not ready for at midnight.
public synchronized Matcher doSomeAwesomeRegexGoodness(String subject)

    return p.matcher(s);


Or just synchronize that method that does your awesome regexy goodness… Who knows… could be right, could be wrong. I hope Paul helps me out with this tomorrow.

Leave a Reply