Collections.synchronizedMap (new LinkedHashMap ()); does not Map threadsafe

I use the following construct to create a thread safe Map .

 Collections.synchronizedMap(new LinkedHashMap()); 

Although I get a ConcurrentModificationException error.

+6
source share
5 answers

Without code, it's hard to guess what the real problem is, but I assume that you are not using the returned collection to do the operations. By javadoc

To ensure consistent access, it is imperative that all access to the support collection is through the returned collection. It is imperative that the user manually synchronizes 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 comply with this advice may result in non-deterministic behavior.

+7
source

Not to distract from other answers here, but this code below shows that simultaneous modification has little to do with actual multi-threaded processing. This caused when you say iterate over the collection, but modify it during the iteration ....

  List list = new ArrayList(); list.add("1"); list.add("2"); Iterator i = list.iterator(); while (i.hasNext()) { Object value = i.next(); // throws java.util.ConcurrentModificationException list.add("another"); } 
+4
source

In short, the decision not to get a ConcurrentModificationException in your code will use ConcurrentHashMap instead of Collections.synchronizedMap(new LinkedHashMap()); . The explanation is here:

According to Nambari, the problem is more difficult to identify without the actual code. Please note that this Map protects only those objects that contain. However, you can change the same instance of an object in a method:

 Map<String, Object> map = new ConcurrentHashMap<String, Object(); //fill the map... map.put("data", new Data()); //and now we have this unsynchronized method that two or more threads can access at the same time public void modifyData(String key, int newDataIntValue) { //this is synchronized by the ConcurrentHashMap Data data = map.get(key); //this isn't //you can get problems here... data.setIntValue(newDataIntValue); } 

The synchronized collection will not save code for these cases. You must synchronize this method yourself.

Additional information: if you are trying to implement a cache library or Flyweight design template , do not reinvent the wheel or use a tried and tested structure, such as ehcache or jboss cache .

+2
source

Please find java doc

Returns a synchronized (thread safe) card supported by the specified card. To ensure consistent access, it is imperative that all access to the support card is through a returnable card. It is imperative that the user manually synchronizes on the returned map when repeating any of their collection types:

  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 follow this advice can lead to deterministic behavior. The returned map will be serialized if the specified map is serializable.

Parameters: m card must be "wrapped" in a synchronized card. Returns: synchronized view of the specified map.

0
source

Synchronized has nothing to do with ConcurrentModificationException, because it can be executed in a single-threaded environment if you try to delete a list item during iteration through a list using the list deletion method.

Synchronization guarantees only sequential access.

0
source

All Articles