Introduced
Parallel operations for new features of C # 4.0
* Parallel operation of Parallel.for-for cycle
* Parallel operation of Parallel.foreach-foreach cycle
* Parallel.Invoke-Invoke multiple tasks in parallel
* Task-tasks, based on the thread pool. It makes us simpler for parallel programming and doesn't care about how the bottom is implemented.
* PLINQ-for parallel operations on data in memory, which means that it only supports parallel operations of LINQ to Object
Example
1, Parallel.For's Demo
Parallel/parallelfor.aspx.cs
Code
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Namespace Csharp.parallel
{
public partial class ParallelFor:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Normal ();
Parallelfordemo ();
}
private void Normal ()
{
DateTime dt = DateTime.Now;
for (int i = 0; i < i++)
{
GetData (i);
}
Response.Write ((DATETIME.NOW-DT). Totalmilliseconds.tostring ());
Response.Write ("<br/>");
Response.Write ("<br/>");
}
private void Parallelfordemo ()
{
DateTime dt = DateTime.Now;
Parallel operation of System.threading.tasks.parallel.for-for loop
System.Threading.Tasks.Parallel.For (0, (i) => {GetData (i);});
Response.Write ((DATETIME.NOW-DT). Totalmilliseconds.tostring ());
Response.Write ("<br/>");
}
private int GetData (int i)
{
System.Threading.Thread.Sleep (100);
Response.Write (i.ToString ());
Response.Write ("<br/>");
return i;
}
}
}
/*
Run Result:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2000.0514
0
13
1
19
7
12
18
6
2
8
10
14
4
16
5
3
15
17
9
11
300.0077
*/