LinkedList checkForComodification错误java

前端之家收集整理的这篇文章主要介绍了LinkedList checkForComodification错误java前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好吧,我在这里尝试做的是让一个方法“运行”一个给定量的“时间”的过程,这个所有接缝在一定程度上工作,但它不断给出这些例子.
这是它给出的第一个执行
  1. Exception in thread "main" java.util.ConcurrentModificationException

然后在exicutio它给出了这个

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

我不知道我在做错了什么我觉得这个并发或什么?如果是这样的话?我认为链表本质上是同步的?也许那就是我搞砸了.

那么这里的任何方式是我使用的方法

  1. public static void runQueueOne(LinkedList<MyProcess> q1,LinkedList<MyProcess> q2,LinkedList<MyProcess> q3,LinkedList<MyProcess> q4,int ct)
  2. {
  3. System.out.println("Running Level One Queue");
  4.  
  5.  
  6. for(MyProcess p : q1)
  7. {
  8. if(p.name.equalsIgnoreCase(q1.getFirst().name))
  9. {
  10. //add 3 millsedonds to the service time
  11. q1.getFirst().serviceTimeTotal += 3;
  12. System.out.println(q1.getFirst().name + " is running");
  13.  
  14. }else
  15. {
  16. //add 3 millseconds to wait time fr the un busy one
  17. p.waitTimeTotal+=3;
  18. }
  19. }
  20.  
  21. for(MyProcess p : q2)
  22. {
  23. p.waitTimeTotal+=3;
  24. }
  25. for(MyProcess p : q3)
  26. {
  27. p.waitTimeTotal+=3;
  28. }
  29. for(MyProcess p : q4)
  30. {
  31. p.waitTimeTotal+=3;
  32. }
  33.  
  34. //calculate all the priority
  35. for(MyProcess p : q1)
  36. {
  37. p.calculatePriority();
  38. switch(p.priority)
  39. {
  40. case 1: break;
  41. case 2: q1.remove(p); q2.add(p); break;
  42. case 3: q1.remove(p); q3.add(p); break;
  43. case 4: q1.remove(p); q4.add(p); break;
  44. }
  45.  
  46. }
  47. ct += 3;
  48. }

这是我在main方法调用它的地方

  1. while(!allProcessDone)
  2. {
  3. //arrival queue
  4. for(MyProcess p :al )
  5. {
  6. addToQueue(qOne,p,currentTime);
  7.  
  8. //cheack to see if all the processes are done
  9. if(p1.isDone == true &
  10. p2.isDone == true &
  11. p3.isDone == true &
  12. p4.isDone == true &
  13. p5.isDone == true &
  14. p6.isDone == true &
  15. p7.isDone == true &
  16. p8.isDone == true &
  17. p9.isDone == true &
  18. p10.isDone == true )
  19. {
  20. //end the loop
  21. allProcessDone = true;
  22. System.out.println("All proccess have been completed");
  23. break;
  24. }
  25.  
  26.  
  27.  
  28. switch (robin)
  29. {
  30. case 1: runQueueOne(qOne,qTwo,qThree,qFour,currentTime); robin = 2;
  31. break;
  32. case 2: runQueueTwo(qOne,currentTime); robin = 3;
  33. break;
  34. case 3 : runQueueThree(qOne,currentTime); robin = 4;
  35. break;
  36. case 4 : runQueueFour(qOne,currentTime); robin = 1;
  37. break;
  38.  
  39. }
  40. }

感谢您对此的任何帮助

解决方法

– 您正在同时访问和修改Collection,这不能直接从for-Each循环完成.

– 使用迭代器解决此问题.

  1. LinkedList<MyProcess> q1 = new LinkedList<MyProcess>();
  2.  
  3. Iterator<MyProcess> iterator = q1.iterator();
  4.  
  5. while (iterator.hasNext()){
  6.  
  7. MyProcess mp = iterator.next();
  8.  
  9. if (mp.name.equals("xyz")){
  10.  
  11. iterator.remove(); // You can do the modification here.
  12. }
  13.  
  14.  
  15. }

猜你在找的Java相关文章