소켓의 ArrayList를 반복하는 동안 문제가 있습니다. 목록의 목적은 마지막 클라이언트를 제외하고 다른 클라이언트가 연결될 때 모든 클라이언트에게 메시지를 보내는 것입니다. ConcurrentModificationException이 발생하고이를 관리하는 방법을 모르겠습니다. 도와주세요!모든 클라이언트에 메시지를 보내는 동안 ConcurrentModificationException이 발생했습니다.
서버 프로그램 :
public void start() {
try {
serverSocket = new ServerSocket(10001);
thread = new Thread(new MessageClient(listOfClients));
thread.start();
while (!isStopped) {
socket = serverSocket.accept();
listOfClients.add(socket);
synchronized (listOfClients) {
listOfClients.notifyAll();
}
g.getTextArea().append(
"Client number " + listOfClients.size()
+ " has connected.\n");
threadClient = new Thread(new SerThread(socket, listOfClients));
threadClient.start();
}
} catch (IOException e) {
}
}
스레드 프로그램 (MessageClient) :
public void run() {
while (true) {
for (Iterator<Socket> cl = listOfClients.iterator(); cl.hasNext();) {
synchronized (listOfClients) {
cur = cl.next();
if (!cur.equals(listOfClients.get(listOfClients.size() - 1))) {
try {
System.out.println("dddddddd");
ous = new PrintWriter(cur.getOutputStream());
ous.println("Client " + listOfClients.size()
+ " has connected.");
ous.flush();
} catch (IOException e) {
JOptionPane
.showMessageDialog(null,
"There was a problem getting your outputstream.");
}
try {
System.out.println("ddd");
listOfClients.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
스택 추적을 붙여 넣습니다. 일반적으로 Failure 컬렉션을 반복하면서 수정하려고하면 ConcurrentModificationException이 발생합니다. 의도적으로 또는 다르게 어디에서 작업하는지 확인하십시오. – Nazgul
@Nazgul 스레드 "스레드 0"java.util.ConcurrentModificationException에 예외 DDD 을 dddddddd 인 java.util.ArrayList $ Itr.checkForComodification (ArrayList.java:886)에서 \t \t에서의 java.util.ArrayList $ (java.lang.Thread.run) (Thread.java:745)에서 \t에 com.omisoft.basic_java.networking_and_gui.task4.MessageClient.run (MessageClient.java:836) 에서 Itr.next 스택 추적입니다. –