【问题标题】:LinkedList checkForComodification error javaLinkedList checkForComodification错误java
【发布时间】:2012-10-12 09:12:36
【问题描述】:

好的,所以我在这里尝试做的是让一个方法在给定的“时间”内“运行”一个进程,这一切都在一定程度上起作用,但它不断给出这些感觉。 这是它给出的第一个 execption

     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)

我不知道我在这里做错了什么,我应该让这个并发还是什么?如果有怎么办?我认为链表本质上是同步的?也许这就是我搞砸的地方。

这里的任何方式都是我使用的方法:

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

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

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;

        }
    }

【问题讨论】:

  • 我会使用 ExecutorService,它是内置的、线程安全的,结合了队列和线程池,并允许您获取单个任务的结果。

标签: java linked-list concurrentmodification


【解决方案1】:

您正在同时访问和修改集合,这不能直接从 for-each 循环中完成。

你必须使用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.
     }
 }

【讨论】:

  • 嗯,我喜欢这种方法,我试过了,但它仍然给我同样的错误Iterator&lt;MyProcess&gt; it = q1.iterator(); while(it.hasNext()) { MyProcess mp = it.next(); mp.calculatePriority(); switch(mp.priority) { case 1: break; case 2: q1.remove(mp); q2.add(mp); break; case 3: q1.remove(mp); q3.add(mp); break; case 4: q1.remove(mp); q4.add(mp); break; } }
  • 从头开始,我让它工作了我没有使用 itorator.remove() 函数我使用的是链表一个
  • @MNM 很高兴你做到了,非常欢迎你
【解决方案2】:

当您在使用 for 循环迭代时尝试从 List 中删除元素时,会发生 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() 方法

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    相关资源
    最近更新 更多