Question: **Joshua Block & co-authors** of the famous **Java Concurrentcy In Practice** put forwards-

This is from *Chapter 5 Building Blocks*-

> 
> ...Since a ConcurrentHashMap cannot be locked for exclusive
>  access.....
> 
> 
> 

What I understand is exclusive access refers to the locking the whole collection with each thread holding a lock for the duration of the operations to make them atomic. 

```
    ConcurrentHashMap< String, String> map = new ConcurrentHashMap< String, String>();

    public void getExclusiveAccess(){
        synchronized (map) {
            //....
        }
    }

```

Even though this would be stupidity to do something like above, but clearly exclusive access is achievable through the same.  

Please suggest.  

Why does the author says that **a ConcurrentHashMap cannot be locked for exclusive access** ?

Answer: 
I have done this and it's achievable however, it only works with this external locking and is not supported by `ConcurrentMap`. If you access the map anywhere without using this synchronization, it won't be exclusive. If you use synchronized everywhere it's not concurrent any more.

The only time this might make sense is if you need exclusive write but not exclusive read.
> 
> clearly exclusive access is achievable through the same
> 
> 
> 

not if some other code uses the map outside a synchronized block (which is perfectly acceptable).

This is different from, say, a `synchronizedMap` where all the methods are synchronized on the map itself - so if you `synchronized(map) {... }` on such a map, you effectively have exclusive access.