Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Perl5Matcher re-entrant?

Thread view: 
Robert Mark Bram - 19 May 2006 07:38 GMT
Hi All,

I have the following code:

private static final Perl5Matcher MATCHER = new Perl5Matcher();

private static boolean validateRegex(
        final String field,
        final Pattern regex) {

    boolean matches = false;
    synchronized (MATCHER) {
        matches = MATCHER.matches(field, regex);
    }
    return matches;
}

My question: I do have many threads accessing this code at about the
same time. Is it worthwhile synchronizing on the matches call? I can't
see anything in the docs to say either way, but I am a bit paranoid
since matches isn't a static call..

Rob
:)
Thomas Hawtin - 19 May 2006 11:14 GMT
> private static final Perl5Matcher MATCHER = new Perl5Matcher();
>
[quoted text clipped - 13 lines]
> see anything in the docs to say either way, but I am a bit paranoid
> since matches isn't a static call..

Looks fine to me. I don't know what Pel5Matcher is.
java.util.regex.Matcher is not thread-safe, so requires the synchronized
block.

You can write the method body more simple as:

        synchronized (MATCHER) {
            return MATCHER.matches(field, regex);
        }

If it is a frequently used method on a multiprocessor machine (even
desktops have multiple hardware threads these days), it may be worth
having a separate matcher for each thread:

    private static final ThreadLocal<Perl5Matcher> MATCHER =
        new ThreadLocal<Perl5Matcher>() {
            @Override
            protected Perl5Matcher initialValue() {
                return new Perl5Matcher();
            }
       };

    private static boolean validateRegex(
        final String field, final Pattern regex
    ) {
        return MATCHER.get().matches(field, regex);
    }

(Usual warning that you wont get class unloading with this sort of use
of ThreadLocal: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6254531)

Tom Hawtin
Signature

Unemployed English Java programmer
http://jroller.com/page/tackline/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.