Collections.synchronizedMap

Once we do Collections.synchronizedMap why again we have to

put it on a synchronized(m) ,

What is the difference if i dont do Collections.synchronizedMap

but use it in synchronized (<hashmap>)?

JavaDOC

--

synchronizedMap

publicstatic Map synchronizedMap(Map m)Returns asynchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map.

It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views:

Map m = Collections.synchronizedMap(new HashMap());

...

Set s = m.keySet();// Needn't be in synchronized block

...

synchronized(m){// Synchronizing on m, not s!

Iterator i = s.iterator();// Must be in synchronized block

while (i.hasNext())

foo(i.next());

}

Failure to followthis advice may result in non-deterministic behavior.

[1422 byte] By [CjavaVMa] at [2007-11-27 0:15:11]
# 1

> Once we do Collections.synchronizedMap why again we

> have to

> put it on a synchronized(m) ,

Because if you don't, then while you're iteratng, another thread could come in and modify the map.

> What is the difference if i dont do

> Collections.synchronizedMap

> but use it in synchronized (<hashmap>)?

You mean synchronized (m)? The difference is you'll have to explicitly synchronize around every put and get.

jverda at 2007-7-11 22:01:44 > top of Java-index,Java Essentials,Java Programming...
# 2

>Because if you don't, then while you're iteratng, another thread could come in and modify the map.

Then what difference it made to the MAP ?

> You mean synchronized (m)? The difference is you'll have to explicitly synchronize around every put and get.

Yes

I feel only synchronized(m) is sufficiaent without Collections.synchronizedMap

If we do synchronized(m) any way two threads can not access m simultaneously.

I want to understand what difference does Collections.synchronizedMap this make ?

CjavaVMa at 2007-7-11 22:01:44 > top of Java-index,Java Essentials,Java Programming...
# 3

> >Because if you don't, then while you're iteratng,

> another thread could come in and modify the map.

>

> Then what difference it made to the MAP ?

If a colleciton is structurally modified while an iteration is going on, it screws up the iteration. Iterators assume that the collection will keep the same set of elements during the entire iteration.

You'll get ConcurrentModificationException, or possibly undefined behavior.

>

> > You mean synchronized (m)? The difference is you'll

> have to explicitly synchronize around every put and

> get.

> Yes

>

> I feel only synchronized(m) is sufficiaent without

> Collections.synchronizedMap

> If we do synchronized(m) any way two threads can not

> access m simultaneously.

Collections.synchronizedMap just makes all the put/get/etc. methods synchronized. We don't need it. It's just a convenience. But if we don't have it, then we'll have to explicitly synchronize every call to put(), get(), size(), etc.

jverda at 2007-7-11 22:01:44 > top of Java-index,Java Essentials,Java Programming...