File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes ConcurrentModificationException inside a synchronized method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "ConcurrentModificationException inside a synchronized method" Watch "ConcurrentModificationException inside a synchronized method" New topic

ConcurrentModificationException inside a synchronized method

xeon costa

Joined: May 10, 2011
Posts: 15


I'm iterating an Arraylist inside a synchronized method, and I've set the Arraylist synchronized inside the method, but I still got the error ConcurrentModificationException. Why this is happening?

Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

I believe some other code at the same time is modifying the same list. Hence the exception.

- Chinna
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4624

Chinna is correct. Some other concurrently running thread is changing the size of 'tips' while your iterator is going over the elements. If you want to iterate over the list, you will have to make sure that no other thread can edit it in the meantime. On this end, you do it by synchronizing on 'tips', but you also need to make sure that the code that is editing the list is synchronized on this list as well.

Note that your code is deadlock prone. If your findTaskFromList() method has to wait to acquire a lock on 'tips', your program will hang if the the thread that has the lock calls any synchronized method on the object running findTaskFromList().

It appears that your method doesn't use any fields, so you can remove the synchronized keyword from the method declaration. It also appears to be a utility method, so make it static.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
xeon costa

Joined: May 10, 2011
Posts: 15

Yes Chinna Eranna, this is the problem.

The problem is that I haven't really understand the synchronized(...){...} block. I thought that putting synchronized(tips) {...}, would say that when a thread is inside this point, the object could not be updated in any other part of the code. But, this is not true. So, I think that the solution is to use a synchronized object.

Is there any other solutions for this case?
Chinna Eranna
Ranch Hand

Joined: Dec 08, 2009
Posts: 174

You can use a synchronized ArrayList or a Vector.
Rob Spoor

Joined: Oct 27, 2005
Posts: 20274

ConcurrentModificationException actually isn't limited to multiple threads. You can even get it in one simple loop:

How To Ask Questions How To Answer Questions
Mykhailo Kozik

Joined: May 12, 2011
Posts: 16

ConcurrentModificationException throws if number of expected modification do not equal to actual.
That's why even simple-thread application can throw it.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3028
Chinna Eranna wrote:You can use a synchronized ArrayList or a Vector.

Even if the problem is caused by access from multiple threads, simply using a synchronized ArrayList (presumably from Collections.synchronizedList()) or Vector is unlikely to solve the problem - at least not by itself. These classes do not provide synchronization at the right level to solve most problems, including this one. Specifically when you iterate through the list, you need additional synchronization. From the API for Collections.synchronizedList():
It is imperative that the user manually synchronize on the returned list when iterating over it:

Failure to follow this advice may result in non-deterministic behavior.

And frankly there are plenty of other times that you need additional synchronization to get proper thread-safe behavior from a synchronizedList() or Vector. These classes cannot generally be trusted to automatically provide thread safety - the user still needs to understand synchronization and use it judiciously.
I agree. Here's the link:
subject: ConcurrentModificationException inside a synchronized method
It's not a secret anymore!