How to change a vector at the same time

I have to ensure the iteration of the vector; there is no update for this vector to avoid a ConcurrentModificationException . I can use a parallel collection. But I just want to try on Vector. Below is the code I wrote.

 public class TestConcurrentModification1 { Vector a = new Vector(); public static void main(String[] args) { final TestConcurrentModification1 obj = new TestConcurrentModification1(); new Thread(){ public void run(){ for(int i = 0; i < 5; i++){ try { Thread.sleep(1); } catch (InterruptedException e) {} obj.a.add(""+i); } System.out.println(obj.a); } }.start(); new Thread(){ public void run(){ try { Thread.sleep(10); } catch (InterruptedException e) { } synchronized (obj.a) { Iterator itr = obj.a.iterator(); while(itr.hasNext()) { obj.a.add("TEST");//java.lang.OutOfMemoryError: Java heap space //itr.remove(); //java.lang.IllegalStateException } } } }.start(); } } 

But the above code throws 1) OutOfMemoryError OR 2) IllegalStateException . Could you explain what causes these two exceptions. And how to achieve my goal of avoiding ConcurrentModificationException on Vector ?

I have to solve this for Java 1.4.2 or earlier.

0
source share
2 answers

One part of your problem:

  Iterator itr = obj.a.iterator(); while(itr.hasNext()) { obj.a.add("TEST");// <- if itr.hasNext() would have returned false in next iteration, now it won't } 

This is an endless loop that increases memory usage at each iteration. Thus, sooner or later you will encounter OutOfMemory.

I suggest using the good old for-loop to insert values. Use an iterator if you really want to repeat something :)

Optional: synchronization with an unconfigured member.

Read more: Iterator.remove throws ...

IllegalStateException - if the next method has not yet been called or the delete method has already been called since the last call to the next method.

And last but not least, the state of the race that Sotirios has already mentioned (+1 for him). Whenever you synchronize, make sure you synchronize every call on a critical resource.

+2
source

You have in your hands the good old conditions of the race.

Your first Thread , with the exception of adding the first element to your Vector , serves absolutely no purpose. You can replace it with

 obj.a.add("first"); 

Beef, as others have noted, is here

 Iterator itr = obj.a.iterator(); while (itr.hasNext()) { obj.a.add("TEST");// java.lang.OutOfMemoryError: Java // heap space // itr.remove(); //java.lang.IllegalStateException } 

itr.hasNext() is implemented as

 public boolean hasNext() { return cursor != elementCount; } 

If the cursor starts at 0 and elementCount is the size of your Vector . This call will never return false . Your while with the loop, adding elements until the program runs out of memory. cursor never moves forward because you never call next() . If you call next() when adding elements directly to Vector , you will get a ConcurrentModificationException .

+2
source

All Articles