【发布时间】:2017-10-21 19:23:41
【问题描述】:
我正在用 C# 创建一个基于 Lucene.Net 的搜索引擎应用程序,它只有一个索引。作为一项要求,我需要为具有多个 (5) 查询的测试运行优化运行时。因此,我想为每个搜索使用一个单独的线程,返回类似于this 帖子的结果。我的代码如下所示:
// load information needs
List<InformationNeed> informationNeeds = FileReader.readInformationNeeds(collectionPath);
// each search has its own thread referenced in this list
List<Thread> threadList = new List<Thread>();
// each search has its own result referenced in this list
List<SearchResult> searchResults = new List<SearchResult>();
foreach (InformationNeed informationNeed in informationNeeds){
// create searchOptions
SearchOptions searchOptions = new SearchOptions(DEBUG_pre_processQuery, informationNeed.getInput());
// run search
SearchResult result = null; // Used to store the return value
var thread = new Thread(
() =>
{
result = startSearch(searchOptions);
Console.WriteLine("end search for IN nr " + informationNeed.getID() + "results = " + result);
//add results to list
searchResults.Add(result);
});
thread.Start();
threadList.Add(thread);
}
// block main thread until all threads finished
foreach (Thread t in threadList){
t.Join();
}
return searchResults;
但是,我得到了一个 Lucene.Net.QueryParser.ParseException see screenshot,在按顺序运行搜索时我没有得到。
如果我有不清楚的地方,请发表评论。对于这个问题,我将不胜感激。
【问题讨论】:
-
您没有同步访问
searchResults。这会导致问题。
标签: c# multithreading lucene.net