Collections.synchronizedMap(new LinkedHashMap()); is not making Map threadsafe

concurrency, java, multithreading, thread-safety

Solution

Without code it is hard to guess what is the real issue, but my guess is, you are not using returned collection to perform operations. As per javadoc

In order to guarantee serial access, it is critical that all access to the backing collection is accomplished through the returned collection. It is imperative that the user manually synchronize on the returned collection when iterating over it:

  Collection c = Collections.synchronizedCollection(myCollection);
     ...
  synchronized(c) {
      Iterator i = c.iterator(); // Must be in the synchronized block
      while (i.hasNext())
         foo(i.next());
  }

Failure to follow this advice may result in non-deterministic behavior.

Problem

I'm using following construct for creating a threadsafe `Map`. ``` Collections.synchronizedMap(new LinkedHashMap()); ``` Though I'm getting `ConcurrentModificationException` error.

Original source

Related problems