-1

I have a loop that runs through some properties , but within that loop must add some properties . The first iteration of the program can run , but from the second iteration happens the following exception:

Exception in thread "main" java.util.ConcurrentModificationException: Iterator: started at 5, now 113
    at org.apache.jena.tdb.sys.DatasetControlMRSW.policyError(DatasetControlMRSW.java:157)
    at org.apache.jena.tdb.sys.DatasetControlMRSW.access$000(DatasetControlMRSW.java:32)
    at org.apache.jena.tdb.sys.DatasetControlMRSW$IteratorCheckNotConcurrent.checkCourrentModification(DatasetControlMRSW.java:110)
    at org.apache.jena.tdb.sys.DatasetControlMRSW$IteratorCheckNotConcurrent.hasNext(DatasetControlMRSW.java:118)
    at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:303)
    at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:303)
    at org.apache.jena.atlas.iterator.Iter$4.hasNext(Iter.java:303)
    at org.apache.jena.atlas.iterator.Iter.hasNext(Iter.java:908)
    at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
    at org.apache.jena.util.iterator.Map1Iterator.hasNext(Map1Iterator.java:49)
    at org.apache.jena.util.iterator.WrappedIterator.hasNext(WrappedIterator.java:90)
    at br.com.edu.dataset.ConstrucaoDataSet.main(ConstrucaoDataSet.java:110)
Java Result: 1

method:

  public ArrayList<Topic> annotate(String originalMarkup) throws Exception {

    PreprocessedDocument doc = _preprocessor.preprocess(originalMarkup);
    Collection<Topic> allTopics = _topicDetector.getTopics(doc, null);

    ArrayList<Topic> bestTopics = _linkDetector.getBestTopics(allTopics, 0.5);

    return bestTopics;
}

.... main ...

Property body = model.getProperty("http://data.linkededucation.org/ns/linked-education.rdf#body");
StmtIterator iter = model.listStatements(null, body, (RDFNode) null);

while (iter.hasNext()) {
    Statement stmt = iter.nextStatement();
    String uri = stmt.getSubject().toString();
    String artigo = stmt.getObject().toString();
    ArrayList<Topic> bestTopics = annotator.annotate(artigo);

    Resource lakPaper = model.createResource(uri);
    for (Topic t : bestTopics) {
        lakPaper.addProperty(DC.subject, model.createResource(keyValueProperty)
                .addProperty(key, t.getTitle())
                .addProperty(value, _df.format(t.getWeight())));
        System.out.println(" - " + t.getTitle() + "[" + _df.format(t.getWeight()) + "]");
    }
}

how to solve that problem?

nbrooks
  • 18,126
  • 5
  • 54
  • 66
Angelo Neves
  • 19
  • 1
  • 5

2 Answers2

3

If it is not possible to refrain from con-current modification in list while iterating over it, in that case you can use CopyOnWriteArrayList, instead of ArrayList, from java.util.concurrent package.

From java doc:

A thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) are implemented by making a fresh copy of the underlying array.

Thus your iterator will never throw ConcurrentModificationException. Read more from here:

https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CopyOnWriteArrayList.html

ankidaemon
  • 1,363
  • 14
  • 20
  • Thanks for the answer . Am New No java .. I wonder How would com CopyOnWriteArrayList.Podria This code help me again ? – Angelo Neves Jun 18 '16 at 11:51
1

Concurrent Modification occurs when you try to modify the "structure" of the collection , you are iterating on, i.e. if you insert or delete elements while iterating on the list.

So to avoid this, use a new collection and perform the insertion and deletion on it accordingly.

yash sachdeva
  • 637
  • 5
  • 13