2012-10-22 70 views
6

好的,所以我在這裏要做的是讓一個方法「運行」一個給定量的「時間」的過程,所有的接縫工作到一定的程度,但它不斷給這些eceptions。 這是第execption它給LinkedList checkForComodification error java

 Exception in thread "main" java.util.ConcurrentModificationException 

再後來就在exicutio它給這個

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) 

我不知道我做錯了,我應該讓這個併發什麼?如果是這樣如何?我以爲鏈表是自然同步的嗎?也許那個讓我搞砸的地方。

那麼這裏任何方式,即時通訊usings方法:

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; 
} 

和這裏就是我稱之爲的主要方法

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,qTwo, qThree, qFour, currentTime); robin = 3; 
       break; 
      case 3 : runQueueThree(qOne,qTwo, qThree, qFour, currentTime); robin = 4; 
       break; 
      case 4 : runQueueFour(qOne,qTwo, qThree, qFour, currentTime); robin = 1; 
       break; 

     } 
    } 

感謝這個

+0

我會用它內置的ExecutorService的,線程安全的,結合了隊列和線程池,讓您獲得的個人任務的結果。 –

回答

8

-你併發訪問和修改的集合,可以不可直接從每個循環完成。

-使用Iterator來解決此問題。

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. 
     } 


} 
+0

以及我喜歡這種方法,我嘗試了它,但它仍然給我同樣的錯誤'Iterator it = q1.iterator(); (it.hasNext()) { MyProcess mp = it.next(); mp.calculatePriority(); switch(mp.priority) 案例1:break; 案例2:q1.remove(mp); q2.add(MP);打破;案例3:q1.remove(mp); q3.add(MP);打破; 案例4:q1.remove(mp); q4.add(MP);打破; } }' – MNM

+0

劃痕,我得到它的工作我沒有使用itorator.remove()函數我正在使用鏈表 – MNM

+0

@MNM我很高興你做到了,而你是最歡迎的 –

4

任何幫助當您嘗試使用for循環遍歷它時嘗試從列表中刪除元素時,會發生ConcurrentModificationException。

我猜你的錯誤是從這些行未來 -

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; 
     } 

} 

要修正這個錯誤,使用iterator.remove()方法