android中的java.util.ConcurrentModificationException(GLThread 23204)

时间:2016-02-24 12:21:34

标签: java android multithreading concurrentmodification

尝试使用Map对象添加对象,我在此函数的第4行获得并发修改异常:

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

我做错了什么?

1 个答案:

答案 0 :(得分:2)

你可以尝试

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