java – Collections. synchronizedMap(new LinkedHashMap()); Map thread is not made safe

I am using the following construct to create a thread safe map

Collections.synchronizedMap(new LinkedHashMap());

Although I received a concurrentmodificationexception error

Solution

Without code, it's hard to guess what the real problem is, but my guess is that you don't use the returned collection to perform the operation According to Javadoc

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 recommendation may lead to uncertain behavior

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>