我从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)
我怎样才能避免这种情况发生?这里有什么好的编码实践?在每次调用SimpleFeatureCollection
方法之前,我是否应该使用shape文件创建.features()
?任何见解将不胜感激。
答案 0 :(得分:2)
当javadocs明确表示您必须在使用后关闭FeatureIterator
,否则资源将耗尽或泄露。你需要使用这样的代码:
FeatureIterator i = featureCollection.features()
try {
while( i.hasNext() ){
SimpleFeature feature = i.next();
}
}
finally {
i.close();
}