18
Sep
04

String.intern() and synchronization

It’s very common that whenever you need to synchronize on a String value (for example, a username) you must synchronize on a common single object for all the intances of Strings that are Object.equals(Object) among them (you need to synchronize concurrent access to the system for the same user). Also it’s very common to choose this common single object as the value of a Hashtable with the keySet being the String values.

Lets code it:

Map lockMap=new Hashtable();
//…
public boolean createWorkspace(String username) {
   synchronize(getCommon(username)) {
       //your stuff
   }
}
//…
//does not work!
private Object getCommon(String value) {
    Object common=lockMap.get(value);
    if(common==null) {
        common=new Object();
        lockMap.put(value,common);
    }
    return common;
}
 

This won’t work because if two thread with the same username has just one line of difference they could return different “common” objects. The following doesn’t work either for the same reason. Two different threads with the same value can enter into the if.

//doesn’t work either!
private Object getCommon(String value) {
    if(lockMap.get(value)==null) {
        lockMap.put(value,new Object());
    }
    return lockMap.get(value);

}

 

So it seems that the only solution is to synchronize the whole method, whether on the lockMap object or on the instance of our code (suppose it’s a singleton). The code should be something like:

private Object getCommon(String value) {
    synchronized(lockMap) {
        Object common=lockMap.get(value);
        if(common==null) {
            common=new Object();
            lockMap.put(value,common);
        }
        return common;

    }

}
 

And you maybe ask: what’s the problem here? Well, the problem is that in some point of our system we are synchronizing (pausing) all threads when we just wanted to pause the threads that have the same username value. And we are doing this with all the users that want to use the system. So we are in a bottleneck scenario, so the problem here is performance.

So, what to do? Well, for a very unknown reason there is a String method that is almost unknown among all Java developers. This method have been in the String class at least since JDK 1.1 (but I’m almost sure I saw it in the very old 1.0.2, but at the moment of writing this post, this version wasn’t available in the Archive: Java Technology Products Download page) The method documentation says

Returns a canonical representation for the string object.
A pool of strings, initially empty, is maintained privately by the class String.
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
It follows that for any two strings s and t,
s.intern() == t.intern()
is true if and only if s.equals(t) is true.
All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the
Java
 Language Specification

Returns:a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.

So, we should code as the following:

public boolean createWorkspace(String username) {
   synchronize(username.intern()) {
       //your stuff
   }
}
 

I used this aproximation about three years ago on an entry point of a online e-banking application, and worked quite well. Now, looking in the web about this solution I found that this was the topic of the Question of the week Number 158 of the developers.sun.com  more or less 30 weeks ago.


0 Responses to “String.intern() and synchronization”



  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Yo mero

My music tag cloud

@justavo


%d bloggers like this: