我是c#.net中多线程编程的新手,我需要帮助将此代码显示转换为C#中的任务并行。
private void thisIsMethodA()
{
//Vertical database
Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
// for each item
foreach (KeyValuePair<int, Bitmap> entry in verticalDB)
{
// We call the depth first search method
dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
}
}
private void dfsPruning(Prefix prefix, Bitmap prefixBitmap, List<int> sn, List<int> inl, int hasToBeGreaterThanForIStep, int m)
{
int maximumPatternLength = 100;
for (int k = 0; k < sn.Count; k++)
{
if (maximumPatternLength > m)
{
dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1);
}
}
for (int k = 0; k < inl.Count; k++)
{
if (maximumPatternLength > m)
{
dfsPruning(prefixIStep, newBitmap, sTemp, iTemp, item, m + 1);
}
}
}
我应该使用下面的代码吗?
使用Task实现是对的吗?你能告诉我我还能做些什么以及如何提高效率?
private void thisIsMethodA()
{
//Vertical database
Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
// for each item
Parallel.ForEach(verticalDB, (entry) =>
{
// We call the depth first search method
dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
});
}
private void dfsPruning(Prefix prefix, Bitmap prefixBitmap, List<int> sn, List<int> inl, int hasToBeGreaterThanForIStep, int m)
{
int maximumPatternLength = 100;
var tasks = new List<Task>();
for (int k = 0; k < sn.Count; k++)
{
if (maximumPatternLength > m)
{
tasks.Add(Task.Factory.StartNew(() => dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1)));
}
}
for (int k = 0; k < inl.Count; k++)
{
if (maximumPatternLength > m)
{
tasks.Add(Task.Factory.StartNew(() => dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1)));
}
}
Task.WaitAll(tasks.ToArray());
}
所以我的问题是,如何将此代码转换为任务并行库。所以.net只关注并行运行这个任务。 dfsPruning()方法是递归的,我需要帮助将此方法转换为任务并行。
非常感谢您抽出时间阅读我的信,我期待着我收到的任何信息。
答案 0 :(得分:0)
您可以做的一件简单事情是使用Parallel.ForEach
来并行thisIsMethodA
而不是dfsPruning
。这将根据您的执行环境处理不同线程上的每个条目,但您需要小心从不同线程修改的数据
private void thisIsMethodA()
{
//Vertical database
Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
// for each item
Parallel.ForEach (vertocalDB, (entry) =>
{
// We call the depth first search method
dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
});
}
答案 1 :(得分:0)
这可以是Parallel.For
的候选者由于循环可能非常递归,我建议根据您的服务器配置设置MaxDegreeOfParallelism,这样就不会创建任务并继续等待线程或处理器释放。
private void thisIsMethodA()
{
//Vertical database
Dictionary<int, Bitmap> verticalDB = new Dictionary<int, Bitmap>();
// for each item
Parallel.ForEach (vertocalDB, (entry) =>
{
// We call the depth first search method
dfsPruning(prefix, entry.Value, frequentItems, frequentItems, entry.Key, 2);
}
}
private void dfsPruning(Prefix prefix, Bitmap prefixBitmap, List<int> sn, List<int> inl, int hasToBeGreaterThanForIStep, int m)
{
int maximumPatternLength = 100;
Parallel.For(0,sn.Count, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (k) =>
{
if (maximumPatternLength > m)
{
dfsPruning(prefixSStep, newBitmap, sTemp, sTemp, item, m + 1);
}
});
Parallel.For(0,inl.Count, new ParallelOptions { MaxDegreeOfParallelism = 10 }, (k) =>
{
if (maximumPatternLength > m)
{
dfsPruning(prefixIStep, newBitmap, sTemp, iTemp, item, m + 1);
}
});
}