项目中要读取琐碎的文件内容,顺序执行比较耗时,所以编写以下下并发读取函数,读取时可以指定线程数,或者根据cpu数调用:
使用代码:
var fileContents = ParallelReadFile(files, Environment.ProcessorCount);
并发读取函数:
public static IDictionary<string, string> ParallelReadFile(string[] files, int maxThread = 4)
{
var fileCount = files.Length;
var section = (int)Math.Ceiling((fileCount * 1.0) / maxThread);
var readResult = new Dictionary<string, string>();
ParallelLoopResult result = Parallel.For(0, maxThread, threadIndex =>
{
var beginIndex = section * threadIndex;
var endIndex = beginIndex + section;
if (endIndex > fileCount)
{
endIndex = fileCount;
}
for (int i = beginIndex; i < endIndex; i++)
{
var filePath = files[i];
var content = File.ReadAllText(filePath);
readResult.Add(filePath, content);
}
});
return readResult;
}