• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

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: 38
  • 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: 65383
248
  • 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: 10501
224
  • 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...
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!