我正在使用以下构造来创建线程安全Map.
Collections.synchronizedMap(new LinkedHashMap());
虽然我收到ConcurrentModificationException错误.
解决方法
没有代码,很难猜出什么是真正的问题,但我的猜测是,你没有使用返回的集合来执行操作.按照
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()); }
不遵循此建议可能会导致非确定性行为.