java.util.ConcurrentModificationException

原程序是这样写的,会报标题的异常 

 

Iterator<ClientThread> it = s.clienMap.keySet().iterator();
              while(it.hasNext()) {
               ClientThread temp= it.next();
                     if(!temp.isAlive())//查看线程的活动状态
                     {
                         ClientThread.ConnectNumber--;                       

 s.clienMap.remove(temp);
                     }

                 }

 

 

只要修改一个地方就不会出现这样的问题,如下:

 

 

Iterator<ClientThread> it = s.clienMap.keySet().iterator();
              while(it.hasNext()) {
               ClientThread temp= it.next();
                     if(!temp.isAlive())//查看线程的活动状态
                     {
                         ClientThread.ConnectNumber--;                       
                         it.remove();

                     }

                 }

这样只是说把当前的这个对象删除掉了。

你可能感兴趣的:(java,活动)