| Author |
ConcurrentModificationException
|
Anshul Kapoor
Greenhorn
Joined: Apr 09, 2010
Posts: 3
|
|
The code shown here gives ConcurrentModificationException . The reason for this exception is that we are modifying the list while the iterator is Iterating it . I was asked to Fix this Exception by only putting code in the FIXME portion of the code . But I am unable to provide a solution . Kindly suggest .
|
 |
Lanny Gilbert
Ranch Hand
Joined: Jun 11, 2002
Posts: 103
|
|
Any reason you can't build the list first, then create the iterator and call the next() method on the iterator??
Or are there project requirements that you must implement which require you to create the iterator, then add new list elements.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Since the exception is thrown at line 9, it's impossible to change that no matter what code you add after line 9.
Edit: Looks like that's wrong. The exception is thrown at line 12, right? I agree with Lanny that (paraphrasing) this is a bad way to do things and you should do it some other way. But actually this is one of those interview questions which bears no relationship to good code in any way, right?
Assuming that, then yes, there's a perfectly simple line of code which can be inserted to make the exception go away. Just remember that in real life your goal wouldn't be to make the exception go away, it would be to dump that bad code.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
The only way to prevent a ConcurrentModificationException on line 12 is to make sure that the iterator created on line 8 will no longer move forward. In other words, the iterator on line 8 and the iterator on line 12 cannot be the same anymore.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
|
Hmm, this is only theoretically, but I think it might be possible to fool the iterator by making Integer.MAX_VALUE adds and removes to the list in between, overflowing the modification counter and setting it back to the number it was before the first add was called. It would only work if the list didn't check for overflow.
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Going to the opposite end of the complexity scale, my suggestion for a line to insert to prevent an exception from being thrown at line 12 would be
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
Considering the iterator is final, Rob's suggestion seems problematic. I like Stephan's suggestion, though I'm not sure if it will work. Meanwhile, here's my hamfisted approach:
Although we could also prevent the exception with a few much simpler techniques:
or
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
Mike Simmons wrote:Considering the iterator is final, Rob's suggestion seems problematic.
I think that Rob's idea was my idea, too. Didn't notice that "final". Okay, I will solve that by having line 12 be in a place where the iterator isn't final after I insert my code.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32708
|
|
|
The List is declared as final too. Otherwise it would have been so easy to reassign the Iterator.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
How about inserting the following:
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
Nice!
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
Way to think outside the box block
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
That was my idea too, but I would have ended the "main" method with a call to your "dummy" method. But obviously that isn't necessary for the question as stated, so your idea is better than mine.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Stephan van Hulst wrote:Hmm, this is only theoretically, but I think it might be possible to fool the iterator by making Integer.MAX_VALUE adds and removes to the list in between, overflowing the modification counter and setting it back to the number it was before the first add was called. It would only work if the list didn't check for overflow.
Actually, if you make not Integer.MAX_VALUE but 2^32 modifications, it's not theoretical - I tried that once, and you will indeed fool the mod count.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
Oh, I meant Integer.MAX_VALUE adds, and Integer.MAX_VALUE removes, which will amount to 2^32 -2 modifications, and then you need one more modification (possibly delete the original offending element) to end up on the previous count.
Hmm, I'm gonna try this one out.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Stephan van Hulst wrote:Oh, I meant Integer.MAX_VALUE adds, and Integer.MAX_VALUE removes, which will amount to 2^32 -2 modifications
2^32 - 1, not - 2.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3053
|
|
|
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
You're right, I was mistaking the first Integer.MAX_VALUE with Integer.MIN_VALUE.
|
 |
 |
|
|
subject: ConcurrentModificationException
|
|
|