I have gone through the below links Problem with synchronizing on String objects? and http://illegalargumentexception.blogspot.com/2008/04/java-synchronizing-on-transient-id.html
Now my question:
instead of synchronizing on the whole map, we tried to synchronize on the userid and that results in some random behavior, if we use intern() it works the approach in the second link also works
Questions:
The best way is to use a ConcurrentHashMap from the java.util.concurrent package. This class has everything you need built right in. Don't re-invent the wheel!
Note: Thilo is right: You must use ConcurrentHashMap's special thread-safe version of put: putIfAbsent()