我已经创建了这个简单的工作线程,通过迭代ArrayList来计算回文。 当我执行行temp_str1 = it.next();时,我得到一个错误。 ArrayList buffer_List没有被任何其他线程使用,因此使用synchronized块无助于此。我查看了以前的问题,但他们没有太多帮助。我急于找到解决这个问题的办法。iterator.next()中的ConcurrentModificationException多线程
这里是我的代码:
private void find_Palindromes(ArrayList<String> buffer_List){
Iterator<String> it = buffer_List.iterator();
String temp_str1, temp_str2;
while(it.hasNext()){
temp_str1 = it.next();
//System.out.println(temp_str1);
it.remove();
if(is_Palindrome(temp_str1)){
to_Shared_Queue(temp_str1);
palin_count++;
}
}
}
编辑代码:加入to_Shared_Queue
private void to_Shared_Queue(String str){
synchronized(shared_queue){
Shared_queue.add(str);
}
}
您的[mcve]请。 –
看看这家伙的回答,可能会帮助你。这是您用来从ArrayList中移除项目的移除方法。 http://stackoverflow.com/questions/15993356/how-iterators-remove-method-actually-remove-an-object – kevto
我敢打赌,你正在修改to_Shared_Queue方法中的buffer_List。 – MGorgon