将跟随Visual Studio 2010一起发布的.NET Framework 4.0将包含很多基于库的对并行计算的支持。包括数据的并行化,任务的并行化等等,这一切都通过一个共同的工作调度器进行管理。这些新的类型和类,将在System.Threading, System.Threading.Tasks, System.Linq, 和 System.Collections.Concurrent这些名字空间中提供。通过这些新的类型和类,开发人员将无需面对如今复杂的多线程开发模式,而可以直接使用.NET Framework,更加高效简便地开发支持并行计算的应用程序,从而更加充分地利用多核CPU的优势,随着计算核心或者处理器的增加,以提升应用程序的性能。
而在.NET Framework中,Task Parallel Library (TPL)是其Parallel Extensions中一个重要组成部分,它提供了一种简便的多线程开发方式,通过它所提供的类或者函数,可以让程序员轻松地实现并行计算。其中,最简单的就是它的Parallel类
Parallel类
Parallel类就是TPL中的一个用于支持并行计算的类。Parallel类提供了诸多的静态函数,只需要简单的函数调用,我们就可以对常用的for循环,foreach循环进行并行化。下面我们通过一些实际的例子,来看看如何利用这个类将我们的应用程序并行化,以吃上多核这“免费的午餐”。
创建示例项目
为了演示如何将一个现有的项目并行化,我们需要先创建一个示例项目。在这个项目中,我们将模拟对数据的串行操作,然后介绍如何利用Parallel类将对数据的串行操作并行化,以充分利用多核CPU的优势,从而提升应用程序的性能。
在Visual Studio 2010中,我们新创建一个Visual C#的控制台应用程序。然后在这个项目中添加一个类Employee,其实现代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ParallelDemo
{
// 职员类
public class Employee
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string Address
{
get;
set;
}
public DateTime HireDate
{
get;
set;
}
public int EmployeeID
{
get;
set;
}
// 模拟对数据的处理
public static decimal Process(Employee employee)
{
Console.WriteLine("Processing {0}", employee.EmployeeID);
// 产生一个随机数
// 用以表示处理当前数据需要的时间
var rand = new Random(DateTime.Now.Millisecond);
var delay = rand.Next(1, 5);
var count = 0;
var process = true;
while (process)
{
System.Threading.Thread.Sleep(1000);
count++;
if (count >= delay)
process = false;
}
return delay;
}
}
// 职员列表类
// 这是我们需要处理的数据
public class EmployeeList : List
{
public EmployeeList()
{
// 将职员添加到列表中
Add(new Employee { EmployeeID = 1, FirstName = "张", LastName = "三", HireDate = DateTime.Parse("1/1/2007") });
Add(new Employee { EmployeeID = 2, FirstName = "李", LastName = "四", HireDate = DateTime.Parse("3/15/2006") });
Add(new Employee { EmployeeID = 3, FirstName = "王", LastName = "麻子", HireDate = DateTime.Parse("6/17/2005") });
Add(new Employee { EmployeeID = 4, FirstName = "赵", LastName = "匡胤", HireDate = DateTime.Parse("3/19/2000") });
Add(new Employee { EmployeeID = 5, FirstName = "钱", LastName = "进", HireDate = DateTime.Parse("7/17/2003") });
Add(new Employee { EmployeeID = 6, FirstName = "孙", LastName = "俊鹏", HireDate = DateTime.Parse("9/13/2005") });
Add(new Employee { EmployeeID = 7, FirstName = "李", LastName = "明", HireDate = DateTime.Parse("12/3/2002") });
Add(new Employee { EmployeeID = 8, FirstName = "周", LastName = "大勇", HireDate = DateTime.Parse("7/1/2008") });
Add(new Employee { EmployeeID = 9, FirstName = "吴", LastName = "明子", HireDate = DateTime.Parse("1/7/2008") });
Add(new Employee { EmployeeID = 10, FirstName = "郑", LastName = "邦万", HireDate = DateTime.Parse("11/1/2001") });
Add(new Employee { EmployeeID = 11, FirstName = "王", LastName = "朝", HireDate = DateTime.Parse("4/21/2006") });
Add(new Employee { EmployeeID = 12, FirstName = "冯", LastName = "玛丽", HireDate = DateTime.Parse("7/19/2006") });
Add(new Employee { EmployeeID = 13, FirstName = "陈", LastName = "良乔", HireDate = DateTime.Parse("3/9/2001") });
Add(new Employee { EmployeeID = 14, FirstName = "褚", LastName = "春晖", HireDate = DateTime.Parse("7/15/2005") });
Add(new Employee { EmployeeID = 15, FirstName = "卫", LastName = "斯理", HireDate = DateTime.Parse("8/6/2003") });
Add(new Employee { EmployeeID = 16, FirstName = "蒋", LastName = "中正", HireDate = DateTime.Parse("5/18/2005") });
Add(new Employee { EmployeeID = 17, FirstName = "沈", LastName = "洋洋", HireDate = DateTime.Parse("8/5/2002") });
Add(new Employee { EmployeeID = 18, FirstName = "韩", LastName = "斌", HireDate = DateTime.Parse("10/1/2006") });
Add(new Employee { EmployeeID = 19, FirstName = "杨", LastName = "雪", HireDate = DateTime.Parse("12/7/2002") });
Add(new Employee { EmployeeID = 20, FirstName = "朱", LastName = "辉", HireDate = DateTime.Parse("3/30/2001") });
DateTime.Parse("12/7/2002") });
Add(new Employee { EmployeeID = 20, FirstName = "朱", LastName = "辉", HireDate = DateTime.Parse("3/30/2001") });
}
}
}
}
}
}