I am trying to understand how locks work in ConcurrentHashMap. My understanding is that in ConcurrentHashMap there are segments and a write requests acquires a lock on a particular segment while there can be simultaneous reads on other segments. But say,
A read request comes in for segment 10, and a write request comes in for the same segment 10. While a request is reading, a write request tries to modify it. How does the write request know it has to wait until a read is finished? Are there two separate locks one for read and one write lock in ConcurrentHashMap or just one lock? Does write have to acquire a read lock too for writing and that's the reason why write waits? If there is only one lock how does simultaneous read work?
Two requests 1 and 2 come in for the same segment 2. Request 1 acquires the lock on segment 1, does the request have to wait until 2 has finished reading?