【发布时间】:2015-06-04 04:21:23
【问题描述】:
所以,我目前正在开发一个支持多客户端的服务器,我有一个线程检查是否有任何套接字连接到给定端口,然后将它们添加到另一个线程用来更新我的所有内容的数组列表中需要与客户端做(更新信息,检查数据输入流,通过服务器发送文本)等等。
客户代码:
public class Loop implements Runnable{
ArrayList<ClientInstance> clientsConnected = new ArrayList<ClientInstance>();
@Override
public void run() {
while(true) {
checkInputStream();
}
}
public void checkInputStream() {
for (ClientInstance s : clientsConnected) {
s.checkInputStream();
}
}
服务器代码:
public synchronized void waitForClient() {
try {
System.out.println("Waiting for client on port: "
+ serverSocket.getLocalPort());
Socket client = serverSocket.accept();
System.out.println("Client Connected! " + client.getInetAddress());
loop.getClientsConnected().add(new ClientInstance(client));
System.out.println("Client added to clients connected! ");
} catch (IOException e) {
e.printStackTrace();
}
}
但是当我运行服务器然后将一个客户端连接到它时它工作正常,但是当我连接另一个客户端时它给了我这个问题:
Exception in thread "Thread-1" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
我该怎么办?
【问题讨论】:
标签: java multithreading arraylist concurrency network-programming