开发者

Java java.util.ConcurrentModificationException error

please can anybody help me solve this problem last so many days I could not able to solve this error. I tried using synchronized method and other ways but did not work so please help me

Error

java.util.ConcurrentModificationException
 at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
 at java.util.AbstractList$Itr.remove(Unknown Source)
 at JCA.startAnalysis(JCA.java:103)
 at PrgMain2.doPost(PrgMain2.java:235)

Code

 public synchronized void startAnalysis() {
        //set Starting centroid positions - Start of Step 1
        setInitialCentroids();
        Iterator<DataPoint> n = mDataPoints.iterator();
        //assign DataPoint to clusters
        loop1:
        while (true) {
            for (Cluster c : clusters)
            {
                c.addDataPoint(n.next());
                if (!n.hasNext())
                    break loop1;
            }
        }

        //calculate E for all the clusters
        calcSWCSS();

        //recalculate Cluster centroids - Start of Step 2
        for (Cluster c : clusters) {
            c.getCentroid().calcCentroid();
        }

        //recalculate E for all the clusters
        calcSWCSS();


       // List copy = new ArrayList(originalList);

        //synchronized (c) {

        for (int i = 0; i < miter; i++) {
            //enter the loop f开发者_JAVA技巧or cluster 1

         for (Cluster c : clusters) {


                for (Iterator<DataPoint> k = c.getDataPoints().iterator(); k.hasNext(); ) {
             //    synchronized (k) {

                 DataPoint dp = k.next(); 


                    System.out.println("Value of DP" +dp);
                    //pick the first element of the first cluster
                    //get the current Euclidean distance
                    double tempEuDt = dp.getCurrentEuDt();
                    Cluster tempCluster = null;
                    boolean matchFoundFlag = false;

                    //call testEuclidean distance for all clusters
                    for (Cluster d : clusters) {

                        //if testEuclidean < currentEuclidean then
                        if (tempEuDt > dp.testEuclideanDistance(d.getCentroid())) {
                            tempEuDt = dp.testEuclideanDistance(d.getCentroid());
                            tempCluster = d;
                            matchFoundFlag = true;
                        }
                        //if statement - Check whether the Last EuDt is > Present EuDt

                    }
                    //for variable 'd' - Looping between different Clusters for matching a Data Point.
                    //add DataPoint to the cluster and calcSWCSS

                    if (matchFoundFlag) {
          tempCluster.addDataPoint(dp);

         //k.notify();  
     //     if(k.hasNext())
          k.remove();


          for (Cluster d : clusters) {
                            d.getCentroid().calcCentroid();
                        }

                        //for variable 'd' - Recalculating centroids for all Clusters

                        calcSWCSS();
                  }

                    //if statement - A Data Point is eligible for transfer between Clusters.
                // }// syn
                 }                 
                //for variable 'k' - Looping through all Data Points of the current Cluster.
            }//for variable 'c' - Looping through all the Clusters.
        }//for variable 'i' - Number of iterations.
     // syn
    }


You can't modify a list while you're iterating it, unless you do it through the Iterator.

From the API: ConcurrentModificationException

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it.

Your code is a mess, so it's hard to figure out what's going on, but I'd check for:

  • Shared references
  • All remove AND add


I think that simply looking up the javadoc for ConcurrentModificationException would have answered your question. Did you try that?

Iterator.remove() is causing the exception, presumably on the linke k.remove(). This means you modified the List it is iterating over while iterating, which is not allowed. So you need to figure out where c.getDataPoints() is changing. I am guessing it is because you eventually find a cluster d, assign to tempCluster, then change its data points (which is eventually the list you are iterating over.


if you need to delete few elements from your list. You can maintain another list like elements to be removed. And finally call removeAll(collection). Of course this is not good for huge data.


Keep few things in mind to avoid concurrent access issues :

First of all the method (startAnalysis) is an instance method. So synchronization will be specific to its instance. So you need to make sure that all the threads trying to access this method must use the same instance to avoid concurrent access issues. If every thread is referring to a different instance, then all the threads will be allowed to execute the method and eventually may lead to concurrency issues.

Secondly, one should always prefer to use Iterator rather the for:each loop to iterate over collections, to avoid concurrent access/modification issues.

Also you can use concurrent collection api classes to avoid concurrency issues. These classes are heavily used in such requirements to avoid concurrent modification issues.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜