aspose file tools
The moose likes Java in General and the fly likes concurrent modification error Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "concurrent modification error" Watch "concurrent modification error" New topic
Author

concurrent modification error

John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192
Hi,
I am using ListInterator interface for iterating through the vector datastructure i have. I have read from Sun docs :
"The Iterators returned by Vector's iterator and listIterator methods are fail-fast: if the Vector is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future. The Enumerations returned by Vector's elements method are not fail-fast. "
My code is sunchronized while accessing vector.
But even then i get concurrent modification error. I do not see any bad behaviour ( i mean nothing wrong is happening)and programs is working as it should be, except this error?
Is there any way i can overcome this error ?
Please help.

Thanks in advance.
Praveen.
Lakshmi Dasari
Ranch Hand

Joined: Mar 03, 2003
Posts: 35
Hi Kumar,
Could you post the source code for better understanding.


Lakshmi
John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192

[ March 26, 2003: Message edited by: kumar cheruvu ]
(edited by Cindy to format code)
[ March 31, 2003: Message edited by: Cindy Glass ]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
There is one problem in immediate evidence: the iterator is retrieved outside the synchronized block, so there is an opportunity for another thread to modify messageTable between calling iterator() and entering the synchronized block.
Another probable problem is that the synchronization will only achieve anything if all the code that might modify messageTable is synchronized as well. And, of course, if processMessage() modifies messageTable you'll also get a ConcurrentModificationException.
The explicit synchronized block is a bit of a "code smell". What I would strongly suggest is not synchronizing on the Map itself, but rather wrapping that map inside your own class that exposes methods that do whatever you want to do. Synchronize the methods on your class. All of them, unless you know what you're doing.
Other things --the if (messageTable == null) will never execute, you'd get a NullPointerException from the previous statement. I would also strongly encourage you not to declare msgiterator or spmsg at the top; their scope becomes much too large which reduces the readability of your code. There is no reason why you wouldn't sayThere are no performance penalties associated with this. There's more strange stuff -- the endless do...while loop -- but I guess you know best what your code is supposed to do
- Peter
John Lincoln
Ranch Hand

Joined: Feb 11, 2003
Posts: 192
Thanks Peter.
 
IntelliJ Java IDE
 
subject: concurrent modification error
 
Threads others viewed
Iterator's add Method()
synchronizedList() VS Vector
An Iterator throws a ConcurrentModificationException
Synchronized data structure question
Looking through collections
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture