Avoiding ConcurrentModificationException on List by making a shallow copy

て烟熏妆下的殇ゞ 提交于 2019-12-05 16:19:03
Steven Schlansker

A ConcurrentModificationException is thrown when a collection changes in a manner which invalidates open iterators. This usually happens when a collection which is not thread safe is accessed by multiple threads (although this is not the only cause)

There is still a small error in your code - to safely access a member which is not itself thread safe, you should synchronize on the getAllPersons method.

Assuming that is fixed -- because you are returning a copy, the collection itself cannot be modified by other callers (each gets their own copy). That means that you can never get a ConcurrentModificationException.

Note that this does not protect you against thread safety issues with your Person class, only the collections themselves. If Person is immutable, you should be OK.

In this case, a better solution would be to directly use a CopyOnWriteArrayList which implements similar semantics, but only copies when you actually write to the list - not every time you read from it.

That's because you are returning a copy of the List rather than the list itself. remove() is the only method which is modifying the actual list, accessible by multiple threads. Threads calling the getAllPersons() method will anyways getting a new list, so if they modify this list, its not going to change the original list. So as your collection is not getting modified concurrently by threads you are not getting ConcurrentModificationException.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!