gaganbm
gaganbm

Reputation: 2843

ConcurrentModificationException for Java LinkedList

I have LinkedList of objects and an iterator. I know that this ConcurrentModificationException is thrown when you try to modify the list while running the iterator. But in my case, I don't understand where this modification is being done.

The iterator looks like this :

private static void insertTasks(Task t) {
    if(eventQueue.size() == 0) {
        eventQueue.addFirst(tsk);
        return;
    }

    int pos = 0;
    while (itr.hasNext()){
   //The line below throws the exception

        if (t.getArrivalTime() <= itr.next().getArrivalTime() ) 
        {           
            break;
        }
        pos++;
    }
}

I am calling this insertTasks method from another method as shown below :

tsk = null;
tsk = new Task(1,"P1",1,4.0f,1.5f,0.0f,8.0f);
insertTasks(tsk);

tsk = null;
tsk = new Task(0,"P0",2,5.0f,2.5f,1.0f,10.0f);
insertTasks(tsk);

The getArrivalTime in the Task objects looks like :

public float getArrivalTime() { return arrivalTime; }

My question is, where am I doing this modification ? The while loop where I run this iterator is not doing any modification. Does it ?

Am I missing something ?

Upvotes: 1

Views: 546

Answers (1)

Augusto
Augusto

Reputation: 29977

I reckon the problem is that itr is a static field in your class and that's creating the issue, as you're adding an element to eventQueue in your second call to insertTasks().

Avoid static fields... program yourself to fear them and avoid them as much as you can :). They evil, and OO unfriendly.

Upvotes: 5

Related Questions