如何将此foreach代码转换为Parallel.ForEach?

我对Parallel.ForEach有点困惑 Parallel.ForEach是什么?它到底做什么?< br > 请不要引用任何MSDN链接。< / p >

这里有一个简单的例子:

string[] lines = File.ReadAllLines(txtProxyListPath.Text);
List<string> list_lines = new List<string>(lines);


foreach (string line in list_lines)
{
//My Stuff
}

如何用Parallel.ForEach重写这个例子?

278928 次浏览
string[] lines = File.ReadAllLines(txtProxyListPath.Text);


// No need for the list
// List<string> list_lines = new List<string>(lines);


Parallel.ForEach(lines, line =>
{
//My Stuff
});

这将导致在循环中并行地解析行。如果你想要一个更详细、更少“面向引用”的Parallel类介绍,我写了一个关于TPL的系列,其中包括平行部分。ForEach

string[] lines = File.ReadAllLines(txtProxyListPath.Text);
List<string> list_lines = new List<string>(lines);
Parallel.ForEach(list_lines, line =>
{
//Your stuff
});

Foreach循环:

  • 迭代是按顺序进行的,一个接一个
  • foreach循环从单个线程运行。
  • foreach循环在.NET的每个框架中都有定义
  • 进程的执行可以是,因为它们是串行运行的
    • 进程2在1完成之前不能启动。进程3在2 &1 .做完了…
    • 李< / ul > < / >
    • 快速进程的执行可以是,因为没有线程开销

平行的。ForEach:

  • 执行是平行进行的。
  • 平行的。ForEach使用多线程。
  • 平行的。ForEach在. net 4.0及以上框架中定义。
  • 进程的执行可以是,因为它们可以并行运行
    • 过程1,2,&3 五月并行运行(参见下面示例中的重用线程)
    • 李< / ul > < / >
    • 由于额外的线程开销,快速进程的执行可以是

下面的示例清楚地演示了传统foreach循环和

Parallel.ForEach(示例)

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
namespace ParallelForEachExample
{
class Program
{
static void Main()
{
string[] colors = {
"1. Red",
"2. Green",
"3. Blue",
"4. Yellow",
"5. White",
"6. Black",
"7. Violet",
"8. Brown",
"9. Orange",
"10. Pink"
};
Console.WriteLine("Traditional foreach loop\n");
//start the stopwatch for "for" loop
var sw = Stopwatch.StartNew();
foreach (string color in colors)
{
Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10);
}
Console.WriteLine("foreach loop execution time = {0} seconds\n", sw.Elapsed.TotalSeconds);
Console.WriteLine("Using Parallel.ForEach");
//start the stopwatch for "Parallel.ForEach"
sw = Stopwatch.StartNew();
Parallel.ForEach(colors, color =>
{
Console.WriteLine("{0}, Thread Id= {1}", color, Thread.CurrentThread.ManagedThreadId);
Thread.Sleep(10);
}
);
Console.WriteLine("Parallel.ForEach() execution time = {0} seconds", sw.Elapsed.TotalSeconds);
Console.Read();
}
}
}

输出

Traditional foreach loop
1. Red, Thread Id= 10
2. Green, Thread Id= 10
3. Blue, Thread Id= 10
4. Yellow, Thread Id= 10
5. White, Thread Id= 10
6. Black, Thread Id= 10
7. Violet, Thread Id= 10
8. Brown, Thread Id= 10
9. Orange, Thread Id= 10
10. Pink, Thread Id= 10
foreach loop execution time = 0.1054376 seconds

使用并行。ForEach例子

1. Red, Thread Id= 10
3. Blue, Thread Id= 11
4. Yellow, Thread Id= 11
2. Green, Thread Id= 10
5. White, Thread Id= 12
7. Violet, Thread Id= 14
9. Orange, Thread Id= 13
6. Black, Thread Id= 11
8. Brown, Thread Id= 10
10. Pink, Thread Id= 12
Parallel.ForEach() execution time = 0.055976 seconds

对于大文件,使用以下代码(您不太需要内存)

Parallel.ForEach(File.ReadLines(txtProxyListPath.Text), line => {
//Your stuff
});

这些台词对我很管用。

string[] lines = File.ReadAllLines(txtProxyListPath.Text);
var options = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 10 };
Parallel.ForEach(lines , options, (item) =>
{
//My Stuff
});

我想补充一些平行选项。如果你没有提到它,默认情况下所有的RAM都将用于此,这可能会在生产中给你带来问题。所以最好在代码中添加最大并行度。

Parallel.ForEach(list_lines, new ParallelOptions { MaxDegreeOfParallelism = 2 }, line =>
{
    

});
var concurrentList = new ConcurrentBag<Dto>();
var opts = new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(Math.Ceiling((Environment.ProcessorCount * 0.65) * 1.0)) };


Parallel.ForEach(data,
opts,
row => {
var processResult = process each row
concurrentList.Add(processResult);
}
);