LinkedList checkforconfirmation error Java

Well, what I'm trying to do here is let a method "run" a given quantitative "time" process. All the joints work to some extent, but it keeps giving these examples

Exception in thread "main" java.util.ConcurrentModificationException

Then in exicutio, it gives this

at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:761)
at java.util.LinkedList$ListItr.next(LinkedList.java:696)
at parta.PartA.runQueueOne(PartA.java:273)

I don't know what I'm doing wrong. I think this concurrency or what? If so? I think the linked list is synchronous in nature? Maybe that's why I screwed up

Then any way here is the method I use:

public static void runQueueOne(LinkedList<MyProcess> q1,LinkedList<MyProcess> q2,LinkedList<MyProcess> q3,LinkedList<MyProcess> q4,int ct)
{
     System.out.println("Running Level One Queue");


     for(MyProcess p : q1)
     {
         if(p.name.equalsIgnoreCase(q1.getFirst().name))
         {
             //add 3 millsedonds to the service time
             q1.getFirst().serviceTimeTotal += 3;
             System.out.println(q1.getFirst().name + " is running");

         }else
         {
             //add 3 millseconds to wait time fr the un busy one
             p.waitTimeTotal+=3;
         }
     }

      for(MyProcess p : q2)
     {
         p.waitTimeTotal+=3;
     }
      for(MyProcess p : q3)
     {
         p.waitTimeTotal+=3;
     }
      for(MyProcess p : q4)
     {
         p.waitTimeTotal+=3;
     }

     //calculate all the priority
     for(MyProcess p : q1)
     {
         p.calculatePriority();
         switch(p.priority)
         {
             case 1: break;
             case 2: q1.remove(p); q2.add(p); break;
             case 3: q1.remove(p); q3.add(p); break;
             case 4: q1.remove(p); q4.add(p); break;
         }

     }
     ct += 3;
}

This is where I call it in the main method.

while(!allProcessDone)
    {
        //arrival queue
        for(MyProcess p :al )
        {
          addToQueue(qOne,p,currentTime);

        //cheack to see if all the processes are done
        if(p1.isDone == true &
           p2.isDone == true &
           p3.isDone == true &
           p4.isDone == true &
           p5.isDone == true &
           p6.isDone == true &
           p7.isDone == true &
           p8.isDone == true &
           p9.isDone == true &
           p10.isDone == true )
        {
            //end the loop
            allProcessDone = true;
            System.out.println("All proccess have been completed");
            break;
        }



        switch (robin)
        {
            case 1: runQueueOne(qOne,qTwo,qThree,qFour,currentTime); robin = 2;
                break;
            case 2: runQueueTwo(qOne,currentTime); robin = 3;
                break;
            case 3 : runQueueThree(qOne,currentTime); robin = 4; 
                break;
            case 4 :  runQueueFour(qOne,currentTime); robin = 1;
                break;

        }
    }

Thank you for any help

Solution

– you are accessing and modifying the collection at the same time, which cannot be done directly from the for each loop

– use iterators to solve this problem

LinkedList<MyProcess> q1 = new LinkedList<MyProcess>();

Iterator<MyProcess> iterator = q1.iterator();

while (iterator.hasNext()){

         MyProcess mp = iterator.next();

         if (mp.name.equals("xyz")){

             iterator.remove();    // You can do the modification here.
         }


 }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>