Although you need to remember that thread safety should also take into account the surrounding code, it seems to you that you are lucky. The fact that Matchers are created using the matcher factory template and the lack of public constructors is a positive sign. Similarly, you use the static compile method to create an encompassing Pattern .
So, in a word, if you are doing something like an example:
Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches();
you should be doing pretty well.
The sequence of actions on the sample code for clarity: note that this example strongly means that the Matcher created in this way is local to the flow with the template and test. Ie, you should not expose Matcher this way for any other threads.
Honestly, this is the risk of any thread safety issue. The reality is that any code can be made unsafe if you try hard enough. Fortunately, there is a wonderful book that teaches us a number of ways that we could ruin our code. If we avoid these errors, we greatly reduce our likelihood of slicing problems.
Bob Cross Sep 01 '09 at 1:11 2009-09-01 01:11
source share