首页 > 代码库 > C# Parallel类的作用
C# Parallel类的作用
System.Threading.Tasks.Parallel是能够以并行的方式迭代数据集合(实现了IEnumerable<T>的对象),它主要提供2个方法:For()和ForEach()
事例:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Net.ConsoleApplication
{
public class Student
{
public string id { get; set; }
public string name { get; set; }
}
public class ParallelDemo
{
private List<Student> list = new List<Student>();
public ParallelDemo()
{
for (int i = 0; i < 1000;i++ )
{
Student s = new Student();
s.id = i.ToString("00000");
s.name = "编号" + s.id;
list.Add(s);
}
}
public void Do()
{
Parallel.ForEach(list, stu => {
System.Console.WriteLine("id: " + stu.id + ",name: " + stu.name);
});
}
}
}