Search code examples
javaconcurrenthashmap

lock() method in ConcurrentHashMap


May be this is silly on my part but I looked at the source code for ConcurrentHashMap and I was not able to see the definition of the method lock() anywhere in that class, but I can see this method being invoked at several occasions. In Eclipse , when I say open declaration on lock(), it shows the class ReentrantLock.lock() , so I am confused how this works ? Where is the object reference of the ReentrantLock for the lock() method invocation ?

 V readValueUnderLock(HashEntry<K,V> e) {
        lock(); // where is this method defined in CHM ?
        try {
            return e.value;
        } finally {
            unlock();
        }
    }

Solution

  • That code fragment is inside an inner class within ConcurrentHashMap called Segment, which extends ReentrantLock