InterviewSolution
Saved Bookmarks
| 1. |
How the ConcurrentHashMap works, how it provides the thread safety? |
|
Answer» The concurrentHashMap works on the principle of segmentation, which divides the HashMap into segments and each SEGMENT allocated to each THREAD and LOCKED for same so other thread will not interfere with each other, the default size of segments in each ConcurrentHashMap is 32. Below is the internal code structure of ConcurrentHashMap. This below code works as an internal class of ConcurrentHashMap. PROTECTED static final class Segment { protected int count; // The count is a COUNTER which update is synchronized protected synchronized int getCount() { return this.count; } protected synchronized void synch() {} } /** Segment Array declaration **/ public final Segment[] segments = new Segment[32]; |
|