问题描述
Lucene鼓励从多个线程重用IndexWriter.
Lucene encourages the reuse of an IndexWriter from multiple threads.
鉴于两个线程可能引用IndexWriter,因此如果线程A在写程序上调用关闭,则线程B将剩下一个无用的写程序.但是据我了解,lucene某种程度上知道另一个线程使用相同的编写器并推迟其关闭.
Given that two threads might have a reference to the IndexWriter, if thread A calls close on the writer, thread B would be left with a useless writer. But to my understanding lucene somehow knows that another thread uses the same writer and defers its closure.
确实是这样吗? Lucene如何跟踪另一个线程使用writer?
Is this indeed the case? How does lucene track that another thread uses the writer?
编辑从答案来看,关闭IndexWriter是不正确的.但这带来了一个新问题:如果保持IndexWriter处于打开状态,则实质上会阻止从另一个JVM对该索引的访问(例如,在集群的情况下,或在许多应用程序之间共享索引的情况下).
EDITJudging from the answers it is not correct to close the IndexWriter. But this poses a new issue: If one keeps an IndexWriter open, essentially blocks access to this index from another JVM (eg in case of a cluster, or a shared index between many applications).
推荐答案
如果一个线程关闭IndexWriter,而其他线程仍在使用它,您将得到无法预测的结果.我们尝试让其他线程命中AlreadyClosedException,但这只是尽力而为(不保证). EG,您也可以轻松地点击NullPointerException.因此,您必须在外部进行同步,以确保不执行此操作.
If one thread closes IndexWriter while other threads are still using it, you'll get unpredictable results. We try to have the other threads hit AlreadyClosedException, but this is just best effort (not guaranteed). EG you can easily hit NullPointerException too. So you must synchronize externally to make sure you don't do this.
最近(目前仅在Lucene的主干中,最终将是4.0)修复了IndexWriter内部的大线程瓶颈,允许段刷新同时运行(以前它们是单线程的).在并发硬件上运行带有许多索引线程的应用程序上,这可以大大提高索引吞吐量.参见 http://blog.mikemccandless.com/2011/详情请参阅05/265-indexing-speedup-with-lucenes.html .
Recently (only in Lucene's trunk right now, to be 4.0 eventually) a big thread bottleneck inside IndexWriter was fixed, allowing segment flushes to run concurrently (previously they were single threaded). On apps running with many indexing threads on concurrent hardware this can give a big boost in indexing throughput. See http://blog.mikemccandless.com/2011/05/265-indexing-speedup-with-lucenes.html for details.
这篇关于Lucene IndexWriter线程安全的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!