-1

我已经创建了这个简单的工作线程,通过迭代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); 
     } 
    } 
+0

您的[mcve]请。 –

+0

看看这家伙的回答,可能会帮助你。这是您用来从ArrayList中移除项目的移除方法。 http://stackoverflow.com/questions/15993356/how-iterators-remove-method-actually-remove-an-object – kevto

+2

我敢打赌,你正在修改to_Shared_Queue方法中的buffer_List。 – MGorgon

回答

-2

这是因为你修改迭代循环的同时在它的。你可以通过从你的数组buffer_List中移除它来完成它。 buffer_List.remove(temp_str1);

+1

这看起来与我们推荐的完全相反。 –