Win a copy of 97 Things Every Java Programmer Should Know this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Why adding an element into List is not allowed while iterating

 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back after a long time...

I understand that modification of a List is dangerous while iterating over it, hence ConcurrentModificationException is thrown.
But I do not understand what can be a possible issue if a thread adds elements to the end of the List while another thread is iterating over it.

Can someone please put light on it?
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for example:

List -> {1,2,3,4}

Thread 1   iterating  in  data 3   , suddenly anther Thread 2 deleting the data 1.     when iterating the data 1 throw Exception, because no data 1.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Joseph.

I can understand the issue when removing elements.
But I do not see any issue when adding elements to the end of the List.
 
Marshal
Posts: 69457
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the size of the List seen by thread 1 will be different from that seen by thread 2.
 
Saloon Keeper
Posts: 12013
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that's the reason though, as you can also have inconsistencies when performing two operations on an unsynchronized list in two different threads when you don't iterate at all.

Despite its name, ConcurrentModificationException has nothing to do with multiple threads, but may even occur when iteration and modification occur in the same thread.

Let's say you iterate over all elements in a collection using Iterator.next(), add another element to the collection, and then call hasNext() on the iterator. Should it return true because there is another element at the end? Should it return false because it already reached the end of the list once?

There's no rule that works well for all iterator implementations. For instance, most Set implementations don't have an ordering, so there is no notion of where the element will be added or how it will affect the iterator.

To avoid insidious bugs caused by subtle differences in iterator behavior, the designers chose to make most iterators throw an exception when they detect that the collection has been modified while the iterator is still in use.
 
Tapas Chand
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your inputs...
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic