2014-12-29 51 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