I am trying to get a grip on Collections here and found an article at Race condition
It states that the below code may lead to race condition :
if (! hashtable.contains(key)) {
hashtable.put(key, value);
}
I understood the explanation there to a certain extent but I have a doubt that since the methods of HashTable are synchronized, how is it possible that while thread1 is executing the put() method acquiring the lock on the entire Hashtable, other thread2 can execute the containskey() method? Aren't both these methods synchronized on the hashtable lock?