我有一個應用程序,它可以爬網並將內容作爲lucene索引文件寫入物理目錄。線程安全寫入Lucene索引文件
當我使用線程來達到這個目的時,我得到了寫入錯誤或由於鎖定而導致的錯誤。
我想使用多個線程並寫入索引文件而不會錯過任何線程的任務。如果我添加t.Join();
t.Start();
後的代碼工作對我來說沒有任何錯誤
DocumentSettings writedoc = new DocumentSettings()
{
Host = Host,
downloadedDocument = downloadDocument
};
Thread t = new Thread(() =>
{
doc.AddDocument(writedoc);
});
t.Start();
:
public class WriteDocument
{
private static Analyzer _analyzer;
private static IndexWriter indexWriter;
private static string Host;
public WriteDocument(string _Host)
{
Host = _Host;
Lucene.Net.Store.Directory _directory = FSDirectory.GetDirectory(Host, false);
_analyzer = new StandardAnalyzer();
bool indexExists = IndexReader.IndexExists(_directory);
bool createIndex = !indexExists;
indexWriter = new IndexWriter(_directory, _analyzer, true);
}
public void AddDocument(object obj)
{
DocumentSettings doc = (DocumentSettings)obj;
Field urlField = new Field("Url", doc.downloadedDocument.Uri.ToString(), Field.Store.YES, Field.Index.TOKENIZED);
document.Add(urlField);
indexWriter.AddDocument(document);
document = null;
doc.downloadedDocument = null;
indexWriter.Optimize();
indexWriter.Close();
}
}
爲了上面的類,我傳遞的價值觀是這樣的。但是這會減慢我的過程,實際上,這等於不使用線程的輸出。
我收到錯誤,如:
Cannot rename /indexes/Segments.new to /indexes/Segments
the file is used by some other process.
任何人都可以幫助我在此代碼?
你會得到什麼錯誤?我們可以看到您正在使用的代碼的一個小例子嗎? – unholysampler 2012-01-30 11:52:09
添加了清晰的代碼 – 2012-01-30 12:31:47