Search code examples
javaexceptionconcurrentmodification

ConcurrentModificationException in iterator.next() I cannot find another solution


I'm currently coding a little net thing and wanted to cicle threw all of my players so I used ArrayLists and Lists but there were alot of Exception so going deep to the problem I replaced them with Iterators but I'm getting Concurrent modification exceptions in all the "it.next()" :/. So if please you can help me understand the problem :).

    @Override
public void run()
{
    StringBuilder l = new StringBuilder();
    Iterator<UUID> pc;
    Iterator<Packet> pa;
    while (Jelly.isSTATUS()) {
        pc = Jelly.getOnlinePlayers().keySet().iterator();
        while (pc.hasNext())
            try {
                System.out.println("ENTERED!");
                Player p = Jelly.getOnlinePlayers().get(pc.next());
                System.out.println(p.getIGN());
                if (Packetsts.containsKey(p) && !Packetsts.get(p).isEmpty())
                    try {
                        pa = Packetsts.get(p).iterator();
                        while(pa.hasNext()) {
                            Packet i = pa.next();
                            for ( String j : i.getData())
                                l.append(j + ",");
                            l.append("es");

                            System.out.println("Data: " + l.toString());
                            byte[] toSendBytes = l.toString().getBytes();
                            int toSendLen = toSendBytes.length;
                            byte[] toSendLenBytes = new byte[4];
                            toSendLenBytes[0] = (byte)(toSendLen & 0xff);
                            toSendLenBytes[1] = (byte)(toSendLen >> 8 & 0xff);
                            toSendLenBytes[2] = (byte)(toSendLen >> 16 & 0xff);
                            toSendLenBytes[3] = (byte)(toSendLen >> 24 & 0xff);
                            PrintStreams.get(p).write(toSendLenBytes);
                            PrintStreams.get(p).write(toSendBytes);

                            Packetsts.get(p).remove(i);
                            l.setLength(0);
                        }
                    } catch ( Exception ex) { }
            } catch (ConcurrentModificationException ex){ ex.printStackTrace(); }
    }
    System.out.println("END!");
}

Thank you :)

UPDATE:

    @Override
public void run()
{
    StringBuilder l = new StringBuilder();
    Iterator<UUID> pc = Jelly.getOnlinePlayers().keySet().iterator();
    Iterator<Packet> pa;
    while (Jelly.isSTATUS())
        if (!Jelly.getOnlinePlayers().keySet().isEmpty()) {
            pc = Jelly.getOnlinePlayers().keySet().iterator();
            while (pc.hasNext()) {
                UUID u = pc.next();
                Player p = Jelly.getOnlinePlayers().get(u);
                if (Packetsts.containsKey(p) && !Packetsts.get(p).isEmpty())
                    try {
                        pa = Packetsts.get(p).iterator();
                        while(pa.hasNext()) {
                            Packet i = pa.next();
                            for ( String j : i.getData())
                                l.append(j + ",");
                            l.append("es");

                            System.out.println("Data: " + l.toString());
                            byte[] toSendBytes = l.toString().getBytes();
                            int toSendLen = toSendBytes.length;
                            byte[] toSendLenBytes = new byte[4];
                            toSendLenBytes[0] = (byte)(toSendLen & 0xff);
                            toSendLenBytes[1] = (byte)(toSendLen >> 8 & 0xff);
                            toSendLenBytes[2] = (byte)(toSendLen >> 16 & 0xff);
                            toSendLenBytes[3] = (byte)(toSendLen >> 24 & 0xff);
                            PrintStreams.get(p).write(toSendLenBytes);
                            PrintStreams.get(p).write(toSendBytes);

                            l = new StringBuilder();
                        }
                        Packetsts.get(p).clear();
                    } catch ( Exception ex) { ex.printStackTrace(); }
            }
        }
    System.out.println("END!");
}

Line "UUID u = pc.next();" please, I really hate concurrent modification exception :/


Solution

  • this is not allowed

    Packetsts.get(p).remove(i);
    

    as you are simultaneously iterating over the ArrayList which you are trying to modify. How about making a copy of it when the loop begins? What is your end goal after removing the elements?