此问题在此处已有答案:
Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop(31个回答)
6天前关闭
我把我的应用程序留了一个晚上,第二天早上注意到这样一个堆栈跟踪:
java.util.ConcurrentModificationException: null
at java.base/java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:756) ~[na:na]
at java.base/java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:788) ~[na:na]
at java.base/java.util.LinkedHashMap$LinkedEntryIterator.next(LinkedHashMap.java:786) ~[na:na]
at ****.MyClass.cleanup(MyClass.kt:150) ~[main/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:568) ~[na:na]
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-6.0.12.jar:6.0.12]
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:96) ~[spring-context-6.0.12.jar:6.0.12]
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264) ~[na:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java) ~[na:na]
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[na:na]
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[na:na]
at java.base/java.lang.Thread.run(Thread.java:833) ~[na:na]
字符串
我试图找到一个根本原因,但我不能。所以让我提供我的代码的简化版本:
@Configuration
@EnableConfigurationProperties(MyConnectionProperties::class)
class MyClass(
private val myProperties: MyConnectionProperties,
) {
private val logger: KLogger = KotlinLogging.logger {}
private val myLock = ReentrantLock()
private val myHolder: MutableMap<String, MyConnectionExtension> = mutableMapOf()
@Scheduled(cron = "...")
fun cleanup() {
val connectionsToClose: MutableList<LDAPConnection> = mutableListOf()
myLock.withLock {
myHolder.forEach { (userDn, connectionExtension) ->
val lastTouchTime = max(
connectionExtension.connection.lastCommunicationTime,
connectionExtension.lastPullingTime,
)
val connectionName = connectionExtension.connection.connectionName
if (lastTouchTime + ldapConnectionProperties.lifetimeMs < Instant.now().toEpochMilli()) {
myHolder.remove(userDn)
connectionsToClose.add(connectionExtension.connection)
}
}
}
connectionsToClose.forEach { connection ->
try {
connection.close()
} catch (e: Exception) {
logger.warn(e) {"....."}
}
}
logger.trace { "job finished" }
}
private fun getConnection(userId: String, password: String): LDAPConnection {
myHolder.withLock {
val connectionExtension = connectionHolder[userId]
val connection = connectionExtension?.connection
return if (connection == null || !connection.isConnected) {
createConnection(userId, password).also {
connectionHolder[userId] = LdapConnectionExtension(
connection = it,
lastPullingTime = Instant.now().toEpochMilli(),
)
}
} else {
connectionExtension.lastPullingTime = Instant.now().toEpochMilli()
connection
}
}
}
...
}
型
只有2个函数使用myHolder
,所以我只提供了它们。从我的Angular 来看,对myHolder
的访问是由锁覆盖的,所以即使map在这里不是并发的,也不会有并发问题。
这是怎么了?
2条答案
按热度按时间zpjtge221#
正如注解所说,你是
remove
从一个map中调用,同时用forEach
迭代它。forEach
旋转一个迭代器,如果你在迭代它的同时改变了map,就会抛出一个ConcurrentModificationException。解决方案是在一个单独的集合中记录您想要移除的所有元素,然后在forEach迭代之后检查这些元素并将它们从Map中移除。
z9smfwbn2#
ConcurrentModificationException异常通常在您尝试遍历集合(如List或Map)时发生,并且集合的结构发生更改。尝试在迭代期间修改集合(而不是通过迭代器自己的remove方法)可能会导致此异常。
在你的代码中,当在cleanup()方法中迭代myHolder并试图修改它时会出现问题。当你调用myHolder.remove(userDn)时,你会改变myHolder的结构,但这是在迭代它时完成的,所以会抛出ConcurrentModificationException。
为了解决这个问题,我们可以在迭代过程中收集要删除的键,然后在迭代完成后删除它们。下面是修复的代码:
字符串
这个修复代码的核心思想是:
在myLock保护的块中迭代myHolder,并收集所有即将关闭的连接和要删除的键。迭代完成后,退出myLock-protected块并删除键,以便在迭代期间不修改集合结构。此属性是正确的,并且它在MyConnectionProperties类中可用。