I get an ConcurrentModificationException error in following situation. The line where this occurs is marked with "<-------- ConcurrentModificationException"
I have a main thread which reads from a list as follow:
List<ThemeCacheIndex> list = Collections.synchronizedList(themeCacheList); synchronized (list) { Iterator<ThemeCacheIndex> it = list.iterator(); while (it.hasNext()) { ThemeCacheIndex themeCacheIndex = it.next(); <-------- ConcurrentModificationException doSomething(); } }
I have a AsyncTask which deletes from this list:
@Override protected String doInBackground(String... params) { someElementsToRemove = calculateWhichElementsToRemove(); for(int i=0 ; i < someElementsToRemove.size() ; i++){ themeCacheList.remove(someElementsToRemove.get(i)); } }
I can imagine, that it comes to a concurrent situation, but I thought to prevent this with a synchronized list on the main thread.
It seems I did not understood the concept of multithreading and shared objects.
Can someone help me out of this problem ? How can I prevent this conflict ?