iterator.next() 中的 ConcurrentModificationException 我找不到其他解决方案
ConcurrentModificationException in iterator.next() I cannot find another solution
我目前正在编写一个小的网络代码,并且想把我所有的播放器都扔掉,所以我使用了 ArrayLists 和 Lists 但是有很多异常所以深入研究这个问题我用 Iterators 替换了它们但是我在所有 "it.next()" 中获取并发修改异常:/。
所以如果你能帮我理解这个问题:)。
@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!");
}
谢谢:)
更新:
@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!");
}
第"UUID u = pc.next();"行,我真的很讨厌并发修改异常:/
这是不允许的
Packetsts.get(p).remove(i);
因为您同时遍历要修改的 ArrayList。在循环开始时复制它怎么样?删除元素后您的最终目标是什么?
我目前正在编写一个小的网络代码,并且想把我所有的播放器都扔掉,所以我使用了 ArrayLists 和 Lists 但是有很多异常所以深入研究这个问题我用 Iterators 替换了它们但是我在所有 "it.next()" 中获取并发修改异常:/。 所以如果你能帮我理解这个问题:)。
@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!");
}
谢谢:)
更新:
@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!");
}
第"UUID u = pc.next();"行,我真的很讨厌并发修改异常:/
这是不允许的
Packetsts.get(p).remove(i);
因为您同时遍历要修改的 ArrayList。在循环开始时复制它怎么样?删除元素后您的最终目标是什么?