1

Trying to add objects using a Map object, I'm getting a concurrent modification exception on the 4th line of this function:

public void reloadResources() {
        taskQueue.add(new GLResorceTask() {    //ConcurrentLinkedQueue<GLResorceTask> taskQueue
            public void perform(GLView gl) {
                for(Entry<Resource, GLResourceLoader> entry : reloadMap.entrySet()) {
                    Resource res = entry.getKey();
                    if(res != null)
                        entry.getValue().load(res);
                }
            }
        });
    }

What am I doing wrong?

1

1 Answer 1

2

you can try

public void reloadResources() {
    taskQueue.add(new GLResorceTask() {    //ConcurrentLinkedQueue<GLResorceTask> taskQueue
        public void perform(GLView gl) { 
        CopyOnWriteArrayList list = new CopyOnWriteArrayList(reloadMap);
            for(Entry<Resource, GLResourceLoader> entry : list.entrySet()) {
                Resource res = entry.getKey();
                if(res != null)
                    entry.getValue().load(res);
            }
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.