dcsimg
ConcurrentModificationException is thrown when using an Iterator....
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sumit_Shrotriya
Posted On:   Friday, December 12, 2003 11:24 AM

Hi, I am getting a really weird error..I have an Iterator on a HashSet; the access to the HashSet objects is only via synchronized methods ..thus according to me..they should not be accessible to other threads while one thread has locked them..even in this situation it is throwing a ConcurrentModificationException when I am iterating on the iterator...please let me know if I have missed something here... Here is my code:: import java.util.*; public class TestStore { private Set newData = Collections.synchronizedSet(new HashSet()); private Set oldData = Collections.synchronizedSet(new HashSet()); private boolean finished = false; public T   More>>

Hi,



I am getting a really weird error..I have an Iterator on a
HashSet; the access to the HashSet objects is only via synchronized methods ..thus according to me..they should not be accessible to other threads while one thread has locked them..even in this situation it is throwing a ConcurrentModificationException when I am iterating on the iterator...please let me know if I have missed something here...

Here is my code::

			

import java.util.*;

public class TestStore {

private Set newData = Collections.synchronizedSet(new HashSet());
private Set oldData = Collections.synchronizedSet(new HashSet());
private boolean finished = false;

public TestStore() {
}

/**
* Threads add some data to the newData set if that data
* has not been read by the user yet...
*/
public synchronized void addData(String data) {
if(!oldData.contains (data))
newData.add(data);
}

/**
* Threads reads the data..and gives the method of the
* expected number of data it needs back...however it
* is possible that the user gets less than it asked
* for.
*/
public synchronized ArrayList getRelationship(int nosofData){
ArrayList result = new ArrayList();

//This was just a lame experiment...
synchronized(TestStore.class) {
try {
Iterator itr = newRels.iterator ();
int nos = 0;

while( itr.hasNext () && (nos++ < nosofData)){
//This is where it is failing...
//It runs one time the next time in the while loop it
// gives me a ConcurrentModificationException.
String tmp = (String)itr.next ();
result.add (tmp);
newData.remove (tmp);
oldData.add (tmp);
}
}catch(Exception ex){
ex.printStackTrace ();
}
return result;
}//end of the sync block
}
}




Any Help would be appreciated..




Thanks,
Sumit    <<Less

Re: ConcurrentModificationException is thrown when using an Iterator....

Posted By:   Michael_Couck  
Posted On:   Wednesday, December 17, 2003 07:19 AM


Didn't try your code really, but I don't see any
possibility of concurrent modification. Try this
however.


import java.util.*;
public class TestStore {
private Set newData= Collections.synchronizedSet(new HashSet());
private Set oldData= Collections.synchronizedSet(new HashSet());
private boolean finished= false;
private static TestStore testStore;
/**
* Implementation of Singleton
*/
public static synchronized TestStore getTestStore() {
return testStore == null ? (testStore= new TestStore()) : testStore;
}
/**
* Private constructor to ensure only one instance
*/
private TestStore() {}
/**
* Threads add some data to the newData set if that data
* has not been read by the user yet...
*/
public synchronized void addData(String data) {
if (!oldData.contains(data))
newData.add(data);
}
/**
* Threads reads the data..and gives the method of the
* expected number of data it needs back...however it
* is possible that the user gets less than it asked
* for.
*/
public synchronized ArrayList getRelationship(int nosofData) {
ArrayList result= new ArrayList();
try {
Iterator itr= newData.iterator();
int nos= 0;
while (itr.hasNext() && (nos++ < nosofData)) {
//This is where it is failing...
//It runs one time the next time in the while loop it
// gives me a ConcurrentModificationException.
String tmp= (String) itr.next();
result.add(tmp);
newData.remove(tmp);
oldData.add(tmp);
}
} catch (Exception ex) {
ex.getMessage();
}
return result;
} //end of the sync block
static int iteration= 0;
public static void main(String[] args) {
final TestStore _testStore= TestStore.getTestStore();
int threadCount= 0;
for (int i= 0; i < 10; i++) {
Thread runner= new Thread(new Runnable() {
TestStore testStore= _testStore;
public void run() {
try {
Thread.sleep(100);
} catch (Exception e) {}
for (int i= 0; i < 100; i++) {
System.out.println("Iteration:" + iteration++);
if (Math.random() > Math.random())
testStore.addData("New Data:" + i);
else
testStore.getRelationship(5);
}
}
});
runner.start();
System.out.println("Thread:" + threadCount++);
}
}
}



Hope this helps.

Michael
About | Sitemap | Contact