- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 java 创建一个多线程聊天服务器。当用户 u1 登录并向用户 u2 发送消息时,如果用户 u2 未连接,则消息将发送到服务器并放入待处理消息的 ArrayList 中。当用户u2连接时,他接收来自服务器的消息,并将消息发送给用户u1作为回执。
这是我的代码:
if (pendingmsgs.size()>0) {
for(Iterator<String> itpendingmsgs = pendingmsgs.iterator(); itpendingmsgs.hasNext();) {
//....parsing of the message to get the recipient, sender and text
String pendingmsg = itpendingmsgs.next();
if (protocol.author != null && protocol.author.equals(recipient)) {
response+=msg;
protocol.sendMsg(sender, "Msg "+text+" sent to "+recipient);
itpendingmsgs.remove();
}
}
}
out.write(response.getBytes(), 0, response.length());
这是 ServerProtocol sendMsg() 方法:
private boolean sendMsg(String recip, String msg) throws IOException {
if (nicks.containsKey(recip)) { //if the recipient is logged in
ClientConnection c = nick.get(recipient); //get the client connection
c.sendMsg(msg); //sends the message
return true;
} else {
/* if the recipient is not logged in I save the message in the pending messages list */
pendingmsgs.add("From: "+nick+" to: "+recip+" text: "+msg);
return false;
}
}
这是 ClientConnection sendMsg() 方法:
public void sendMsg(String msg) throws IOException {
out.write(msg.getBytes(), 0, msg.length());
}
其中 out 是一个输出流。
当用户 u1 登录时,向未登录的用户 u2 发送一条消息,然后用户 u1 离开,当用户 u2 登录时,他没有收到消息,我收到此异常:
Exception in thread "Thread-2" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.remove(Unknown Source)
at ChatServer$ClientConnection.run(ChatServer.java:400)
at java.lang.Thread.run(Unknown Source)
第 400 行是
itpendingmsgs.remove();
我尝试过使用 CopyOnWriteArrayList 但它仍然不起作用。
最佳答案
CopyOnWriteArrayList.iterator()
doesn't support remove()
。您可能应该使用 Collections.synchronizedList(ArrayList)
(按照 Javadoc 中的规定,在迭代期间正确锁定)。
这确实是允许一个线程添加到列表而另一个线程迭代删除元素的最简单方法。
关于java - 多线程聊天服务器中迭代器出现 ConcurrentModificationException 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10707161/
这个问题在这里已经有了答案: Why is a ConcurrentModificationException thrown and how to debug it (8 个答案) 关闭 3 年前。
这个问题已经有答案了: Concurrent Modification exception [duplicate] (9 个回答) 已关闭 8 年前。 在oldSet.removeAll(已删除);抛
这个问题在这里已经有了答案: Concurrent Modification exception [duplicate] (9 个回答) 关闭 9 年前。 我的程序抛出 ConcurrentModi
我有以下结构: public class Object1{ private HashMap myMap; ... public void cancelItem(Item o)
在本例中使用 list.remove((Object)93) 会导致 ConcurrentModificationException: List list = new ArrayList<>(); l
更新应用程序后,我收到来自 Firebase 的错误几乎在所有 Activity 中。 Fatal Exception: java.lang.RuntimeException: Unable to s
我有大量的东西,一个重复迭代它们的线程,以及一个偶尔删除或添加单个东西的单独线程。事物在同步链表中: private List things = Collections.synchronizedLis
在 DTO 中我有 public class ProductTypesDto extends BaseDto { private List colors = new ArrayList<>();
我正在创建一个 Swing 应用程序来制作游戏。它在屏幕外的随机位置创建图像,当它们离开屏幕时我想将它们删除。请看一下代码片段: public void checkTrolls(){ //CAUSES
我在向 Android 表插入数据时遇到问题。这是我的 Dao 函数: @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(frei
我有一个 Grails (2.4.3) 应用程序,它使用 PersistenceListener 来监听 GORM 事件。 PersistenceListener 工作正常。在 PreUpdate 事
基于 Spring Boot、spring-data-jpa 的大型 REST Web 应用程序。我有 PersonEntity ,它与 ParentEntity 具有 ManyToOne 关系。关系
我有一个作业要编写一个 Java 程序,该程序将读取上下文无关语法并返回所有非终结符的 FirstSet。我使用 FirstSet() 方法采用了递归方法: public static ArrayLi
我收到java.util.ConcurrentModificationException,但我不知道为什么。 在 Logcat 中,它指向此代码,但我没有看到任何可能导致 ConcurrentModi
我在以下情况下收到 ConcurrentModificationException 错误。线路发生这种情况的地方标有“ list = Collections.synchronizedList(them
自过去两个小时以来,我一直在尝试解决此异常..我得到了抛出异常的代码行..但没有找到解决方案..请帮助我发生错误@ mUsers.add(user);//在其他部分 private void read
我有以下代码: List list = getItems(); //where getItems() returns List do { list.add(ad
我得到ConcurrentModificationException当迭代 map 的内容时 for (String sourceKey : sMap.getContent().keySet(
代码是葡萄牙语的,对此我很抱歉。 我在这里读到另一个问题,因为我正在使用progSelecionada.remove(),所以抛出了异常,所以我更改为iterator.remove()但错误仍然存
我目前正在用 Java 编写多人游戏。我当前的代码(即出现错误)是这样的。 @Override public void onClose(WebSocket conn, int code, String
我是一名优秀的程序员,十分优秀!