• 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
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Why adding an element into List is not allowed while iterating  RSS feed

 
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: 35
  • 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: 64172
215
  • 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: 10136
214
  • 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...
 
That is a really big piece of pie for such a tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!