if (IOThread.getInstance().clientsToAdd.size() > 0) {
for (Client client : IOThread.getInstance().clientsToAdd) {
clients.add(client);
IOThread.getInstance().removeAddedClient(client);
}
}
当它在我的主循环中运行时,我会抛出此错误:
[DEBUG]: Added new connection: 79.142.74.45
Exception in thread "Thread-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at Server.run(Server.java:35)
at java.lang.Thread.run(Thread.java:744)
此方法抛出错误:
public void removeClient(Client client) {
Iterator<Client> clientIterator = clients.iterator();
while (clientIterator.hasNext()) {
Client currentClient = clientIterator.next();
if (client.equals(currentClient)) {
clientIterator.remove();
}
}
}
答案 0 :(得分:1)
此行会导致错误:
IOThread.getInstance().removeAddedClient(client);
您正在循环访问客户端。
请改用iterator.remove()
。