They are not exactly the same - you're locking on different objects in each of the examples.
In the first example, you're locking on
object.
In the first part of the second example, you're locking on the
Test object that you'd call the
obtainLock method on.
In the second part of the second example, you're locking on the Class object of class Test.
If there's nothing else in class Test, then in the end all three variants have the same effect - you're locking so that only one thread at a time will be performing the code that's synchronized. Note that since you're using ConcurrentHashMap, the locking is unnecessary - ConcurrentHashMap is already designed to be thread-safe.
Chandra shekar M wrote:If we have more then one static variable to be synchronized then we need to go for class level lock or we can go for static variable level lock right?
The answer to this question is not a simple yes or no; it depends on what the expected functionality of the class is. If you need to do two actions that always have to be done together, without the chance of another thread doing something else in between the two actions, then you'll need to synchronize.