Java 调用geotools';SimpleFeatureCollection.features()一次过多会导致;超过了最大锁计数;

Java 调用geotools';SimpleFeatureCollection.features()一次过多会导致;超过了最大锁计数;,java,geotools,Java,Geotools,我从硬盘上的形状文件创建了一次geotools”SimpleFeatureCollection。然后我多次调用它的.features()方法。到目前为止,我认为这是一种良好的做法,但似乎并非如此。在多次调用feature方法之后,我收到 Exception in thread "main" java.lang.Error: Maximum lock count exceeded at java.util.concurrent.locks.ReentrantReadWriteLock$Sync.

我从硬盘上的形状文件创建了一次
geotools
SimpleFeatureCollection
。然后我多次调用它的
.features()
方法。到目前为止,我认为这是一种良好的做法,但似乎并非如此。在多次调用feature方法之后,我收到

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)
线程“main”java.lang中出现异常。错误:超过最大锁计数 在java.util.concurrent.locks.ReentrantReadWriteLock$Sync.fullTryAcquireShared(ReentrantReadWriteLock.java:528) 位于java.util.concurrent.locks.ReentrantReadWriteLock$Sync.tryAcquireShared(ReentrantReadWriteLock.java:488) 位于java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireShared(AbstractQueuedSynchronizer.java:1282) 位于java.util.concurrent.locks.ReentrantReadWriteLock$ReadLock.lock(ReentrantReadWriteLock.java:727) 位于org.geotools.data.shapefile.files.ShpFiles.acquisitionead(ShpFiles.java:358) 位于org.geotools.data.shapefile.files.ShpFiles.getReadChannel(ShpFiles.java:789) 位于org.geotools.data.shapefile.shp.ShapefileReader.(ShapefileReader.java:253) 位于org.geotools.data.shapefile.ShapefileSetManager.openShapeReader(ShapefileSetManager.java:51) 位于org.geotools.data.shapefile.ShapefileFeatureSource.getReaderInternal(ShapefileFeatureSource.java:263) 位于org.geotools.data.shapefile.ShapefileFeatureStore.getReaderInternal(ShapefileFeatureStore.java:124) 位于org.geotools.data.store.ContentFeatureSource.getReader(ContentFeatureSource.java:563) 位于org.geotools.data.store.ContentFeatureCollection.features(ContentFeatureCollection.java:165) 我怎样才能避免这种情况发生?这里的良好编码实践是什么?在调用其
.features()
方法之前,是否每次都要使用形状文件创建
SimpleFeatureCollection
?如果您有任何见解,我们将不胜感激。

如前所述,您必须在使用后关闭
功能迭代器
,否则资源将耗尽或泄漏。您需要使用如下代码:

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

尤里卡!这就解决了问题。非常感谢你。我以前从未关闭过迭代器,但总是有第一个迭代器。迭代器通常是要关闭的,还是只需要关闭这个迭代器?只关闭这些迭代器,因为它们下面有真正的资源