2014-12-29 54 views
0

提供我想从多个线程读取同一个IndexReader实例,大概是一个单例实例,这将如何影响性能?是否会出现“读取锁定”或任何可能会损害多线程可能性的内容?Lucene IndexReader如何在多线程环境下使用?

在多线程环境中是否有任何最佳实践使用相同的IndexReader?

+0

据我所知,索引阅读器是线程安全的,只有在同时有多个索引编写器时才会创建LOCK。可能的重复问题 - http://stackoverflow.com/questions/8878448/lucene-good-practice-and-thread-safety – Rushik

回答

2

按照documentation

IndexReader instances are completely thread safe, meaning multiple threads can call any 
of its methods, concurrently. If your application requires external synchronization, 
you should not synchronize on the IndexReader instance; use your own (non-Lucene) 
objects instead. 

但是,你想考虑的另一件事是,如果的IndexReader是由多个线程,并在这一过程中指数得到更新共享,更新的索引不会得到体现,直到你创建一个新的indexreader实例。

+0

我还补充说,搜索时没有涉及到任何一个锁。这可以归功于索引存储的方式,以一次写入(不可变)代文件形式存在。 – jpountz