繁体   English   中英

如何将此代码转换为任务并行库

[英]How to convert this code to Task Parallel Library

我是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);
            }
        }

    }

我应该在下面使用代码吗?

使用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()方法是递归的,我需要帮助才能将此方法转换为并行任务。

非常感谢您抽出宝贵的时间阅读我的来信,我期待收到的一切。

您可以做的一件简单的事情是使用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);
    });
}

这可能是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);
        }
    });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM