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:
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.
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:
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:
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.