Is Java Regex Thread Safe?

Viewed 56145

I have a function that uses Pattern#compile and a Matcher to search a list of strings for a pattern.

This function is used in multiple threads. Each thread will have a unique pattern passed to the Pattern#compile when the thread is created. The number of threads and patterns are dynamic, meaning that I can add more Patterns and threads during configuration.

Do I need to put a synchronize on this function if it uses regex? Is regex in java thread safe?

5 Answers
Related