Thursday, November 5, 2009

Preventing ConcurrentModificationExceptions!

I have encountered several times now, and its very annoying since the error happens randomly. After a bit of googling, I found out that using unsynchronized JAVA collections yields unpredictable results. I am hoping that synchronizing my JAVA collections manually could solve this problem. To do this, I will have to use the ready made JAVA Collections synchronize wrapper:

For example if I use the List interface like this before:

List list = new ArrayList();

I will have to wrap the ArrayList implementation using the Collections synchronize method:

List list = Collections.sychronizedList(new ArrayList);


List list = new ArrayList();
list = Collections.sychronizedList(list);

I hope this works. I will refactor my code now and will update this post once I find out what happens.

EDIT: It works! You just have to synchronize the list before iterating through them :)

1 comment:

  1. yeah do that. xP should have done that before posting! :))