[英]How to convert this code to Task Parallel Library
I'm new to multithread programming in c# .net, I need help to convert this code show bellow to Task parallel in C#. 我是c#.net中多线程编程的新手,我需要帮助将下面的代码转换为C#中的Task并行。
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);
}
}
}
Should I use code below? 我应该在下面使用代码吗?
Is it Right to implement using Task?, Can you suggest me what more can I do and how can i improve the efficiency more? 使用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());
}
So my question is, How to convert this code to Task Parallel Library. 所以我的问题是,如何将此代码转换为任务并行库。 So that .net only take care to run this tasks parallel. 因此,.net仅注意并行运行此任务。 dfsPruning() method is recursive, I need help to convert this method to task parallel. dfsPruning()方法是递归的,我需要帮助才能将此方法转换为并行任务。
Thank you very much for taking the time to read my letter and I look forward to anything I may receive. 非常感谢您抽出宝贵的时间阅读我的来信,我期待收到的一切。
One simple thing you can do is to use Parallel.ForEach
to parallelize the thisIsMethodA
rather than dfsPruning
. 您可以做的一件简单的事情是使用Parallel.ForEach
并行化thisIsMethodA
而不是dfsPruning
。 This will process each entry on a different thread depending on your execution environment,but you need to be careful with data being modified from different threads 这将根据您的执行环境在不同的线程上处理每个条目,但是您需要小心从不同线程修改的数据
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);
});
}
This can be a candidate for Parallel.For 这可能是Parallel.For的候选人
As the loop might be very recursive I recommed to set MaxDegreeOfParallelism based on your server configuration, so that there should not be task created and goes on waiting for threads or processor to release. 由于循环可能是非常递归的,因此我建议根据您的服务器配置来设置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);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.