Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android ConcurrentModificationExection_Java_Android_Multithreading - Fatal编程技术网

Java Android ConcurrentModificationExection

Java Android ConcurrentModificationExection,java,android,multithreading,Java,Android,Multithreading,我试图将我的起始线程存储在列表数组中,但这会导致ConcurrentModificationExeption。有人能帮忙吗 TimerTask timerTask = new TimerTask() { @Override public void run() { getData(parameters); } }; ArrayList<TimerTask> threadList = Thread

我试图将我的起始线程存储在
列表数组中
,但这会导致
ConcurrentModificationExeption
。有人能帮忙吗

TimerTask timerTask = new TimerTask() {

        @Override
        public void run() {
            getData(parameters);

        }
    };
    ArrayList<TimerTask> threadList = Threads.getInstance();
    if(!threadList.isEmpty()){
        for (Iterator<TimerTask> it = threadList.iterator(); it.hasNext(); ) {
            TimerTask timerT = it.next();
            boolean found = false;
            if (timerTask.equals(timerT)){
                found = true;
            }
            if(!found){
                threadList.add(timerTask);
                Timer timer = new Timer();
                timer.scheduleAtFixedRate(timerTask, 0, 10000);
            }
        }
    }else{
        threadList.add(timerTask);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(timerTask, 0, 10000);
    }
TimerTask TimerTask=new TimerTask(){
@凌驾
公开募捐{
获取数据(参数);
}
};
ArrayList threadList=Threads.getInstance();
如果(!threadList.isEmpty()){
for(Iterator it=threadList.Iterator();it.hasNext();){
TimerTask timerT=it.next();
布尔值=false;
if(timerTask.equals(timerT)){
发现=真;
}
如果(!找到){
threadList.add(timerTask);
定时器=新定时器();
timer.scheduleAtFixedRate(timerTask,0,10000);
}
}
}否则{
threadList.add(timerTask);
定时器=新定时器();
timer.scheduleAtFixedRate(timerTask,0,10000);
}

避免多线程环境中的ConcurrentModificationException:

1您可以将列表转换为数组,然后在数组上迭代。这种方法适用于中小型列表,但如果 如果列表很大,则会对性能产生很大影响

2。通过将列表放入同步块,可以在迭代时锁定列表。不建议使用此方法,因为它会 停止多线程的好处

3。如果您使用的是JDK1.5或更高版本,则可以使用ConcurrentHashMap和CopyOnWriteArrayList类。它是 建议的方法

建议:按照第一条建议,在临时数组中添加所有元素,并将临时数组复制到for循环之后的列表中


在对
数组列表进行迭代时,不能将元素添加到该列表中(除非通过zapl所述的方法进行添加)

for(Iterator it=threadList.Iterator();it.hasNext();){
/* [..] */
如果(!找到){
threadList.add(timerTask);//问题出在这里
定时器=新定时器();
timer.scheduleAtFixedRate(timerTask,0,10000);
}
}
你应该:

  • 使用ListIterator。(我建议你这样做)
  • 或者列出要添加的所有元素,然后将它们添加到
    for
    循环之外

问题源于您在迭代列表时修改列表。如果要删除元素,可以使用
iterator.remove()
安全地删除元素,但我不知道在列表上迭代时如何安全地添加元素。也许你可以列出要添加的元素,然后将它们全部添加到末尾。@ArnaudDenoyelle我想op不想删除任何元素。op想要向列表中添加元素。你说的“复制”是什么意思?@pumukel这意味着已经提出了类似的问题。此用户建议您在StackOverflow中进行研究,因为您可能在不创建新问题的情况下找到了答案。为什么无法使其同步?比如:public或者在循环中使用@zapl我不知道这个,谢谢@ArnaudDenoyelle阵列是线程安全的吗?因为从我收到的帖子上可以看出that@KickButtowski我不知道。ConcurrentModificationException具有误导性:它与线程安全无关。@ArnaudDenoyelle您能就我的答案plz给出您的意见吗?
for (Iterator<TimerTask> it = threadList.iterator(); it.hasNext(); ) {
  /* [..] */
  if(!found){
    threadList.add(timerTask); //Here is the problem
    Timer timer = new Timer();
    timer.scheduleAtFixedRate(timerTask, 0, 10000);
  }
}