• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

if two procesess updating a Collection at a time then which exception will raise

 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[red]If two process in application updating one java.util.List object then which kind of exception will be raised?[/red]

help me please.
 
Rajasekar Krishnan
Greenhorn
Posts: 16
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shyam,

ArrayList is not ThreadSafe.

When two threads are accessing same list, alteast the list accessing part need to be synchronized.

Otherwise for example, one thread might remve a object from the list, in the mean time another thread tries to get the same object from the list, in this cases you may get the Exception IndexOutOfBoundsException.

Thanks,
Rajasekar.
 
Rob Spoor
Sheriff
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about you try it?

A number of exceptions can be thrown, depending on the collection and the way the updates overlap. Without proper synchronization an ArrayList's internal structure can become corrupted, and then any RuntimeException can be thrown. ArrayIndexOutOfBoundsException is probably the most common.

Of course, even with synchronization, ConcurrentModificationException is always a possibility when iterating and modifying at the same time. You can even do that using one single process:
 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rajashekar,
 
shyam sunder prasad
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou Greenhorn
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic