Question

I have looked around and cannot find anything that matches or that fixes my problem. I am trying to make a very basic server system right now that will go onto something more advanced. For some reason if more than one client tries to join it will crash with this error message.

Exception in thread "messageRecieveThread" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
at java.util.ArrayList$Itr.next(ArrayList.java:791)
at zx.server.threads.connections.Message.run(Message.java:23)
at java.lang.Thread.run(Thread.java:724)

Here is my message class so far:

package zx.server.threads.connections;

import java.io.IOException;
import java.net.Socket;
import java.util.Iterator;

import zx.server.communication.handle.Processing;
import zx.server.storage.severSide.Store;

public class Message implements Runnable {
private Thread thread;

public Message() {
    thread = new Thread(this);
    thread.setName("messageRecieveThread");
    thread.start();
}

@Override
public void run() {
    while (true) {
        for (Iterator<Socket> it = Store.getAll().iterator(); it.hasNext();) {
            Socket s = it.next();
            try {
                if (s.getInputStream().available() != 0) {
                    byte[] buffer = new byte[s.getInputStream().available()];
                    s.getInputStream().read(buffer);
                    new Processing(s, buffer);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

}

The line that is supposedly causing the error is this one:

Socket s = it.next();

Any help would be immensely appreciated.
Thanks,
~Ryan

Was it helpful?

Solution

Your problem isn't on the shown code. Your problem is on you class Store.

EXPLANATION: What is happening is that while you iterate over Store.getAll() sockets, another socket is being inserted or removed from the same structure retrieved by Store.getAll() (much likely subclass of java.util.Collection - e.g.: LinkedList, ArrayList,HashMap, etc..).

REASON: You cannot iterate over Collection while the same collection is being modified. See ConcurrentModificationException javadocs

SOLUTION: Anyway, to solve your problem on the snippet shown, I suggest that you clone the structure returned by Store.getAll(). But be aware that this structure will be obsolete on its content, because there are sockets being inserted or removed in your Store class.

TIP: (this won't solve your problem but will help you to program fashionably) To iterate over elements from some structure use this in Java:

for(Socket s : Store.getAll()) {
    try {
        if (s.getInputStream().available() != 0) {
            byte[] buffer = new byte[s.getInputStream().available()];
            s.getInputStream().read(buffer);
            new Processing(s, buffer);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top