gpt4 book ai didi

java - 多次调用 geotools 的 SimpleFeatureCollection.features() 导致 "Maximum lock count exceeded"

转载 作者:行者123 更新时间:2023-11-30 08:35:29 32 4
gpt4 key购买 nike

我从 HDD 上的形状文件创建了一次 geotools' SimpleFeatureCollection。然后我多次调用它的 .features() 方法。到目前为止,我认为这是一种很好的做法,但事实似乎并非如此。多次调用特征方法后,我收到了

Exception in thread "main" java.lang.Error: Maximum lock count exceeded
at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.fullTryAcquireShared(ReentrantReadWriteLock.java:528)
at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryAcquireShared(ReentrantReadWriteLock.java:488)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireShared(AbstractQueuedSynchronizer.java:1282)
at java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.lock(ReentrantReadWriteLock.java:727)
at org.geotools.data.shapefile.files.ShpFiles.acquireRead(ShpFiles.java:358)
at org.geotools.data.shapefile.files.ShpFiles.getReadChannel(ShpFiles.java:789)
at org.geotools.data.shapefile.shp.ShapefileReader.<init>(ShapefileReader.java:253)
at org.geotools.data.shapefile.ShapefileSetManager.openShapeReader(ShapefileSetManager.java:51)
at org.geotools.data.shapefile.ShapefileFeatureSource.getReaderInternal(ShapefileFeatureSource.java:263)
at org.geotools.data.shapefile.ShapefileFeatureStore.getReaderInternal(ShapefileFeatureStore.java:124)
at org.geotools.data.store.ContentFeatureSource.getReader(ContentFeatureSource.java:563)
at org.geotools.data.store.ContentFeatureCollection.features(ContentFeatureCollection.java:165)

如何避免这种情况发生?这里有什么好的编码习惯?我是否应该在每次调用其 .features() 方法之前使用形状文件创建 SimpleFeatureCollection?任何见解将不胜感激。

最佳答案

作为javadocs明确您必须在使用后关闭 FeatureIterator 否则资源将耗尽或泄漏。您需要使用这样的代码:

FeatureIterator i = featureCollection.features()
try {
while( i.hasNext() ){
SimpleFeature feature = i.next();
}
}
finally {
i.close();
}

关于java - 多次调用 geotools 的 SimpleFeatureCollection.features() 导致 "Maximum lock count exceeded",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38177085/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com