在NET Framework 中在庫的層次上微軟提供了大量的新內容來幫助程序員完成應用程序的並行化其中包括Parallel LINQ(PLINQ)Task Parallel Library(TPL)和Coordination Data Structures這裡我們就先來介紹一下最簡單最常用的TPL
將跟隨Visual Studio 一起發布的NET Framework 將包含很多基於庫的對並行計算的支持包括數據的並行化任務的並行化等等這一切都通過一個共同的工作調度器進行管理這些新的類型和類將在SystemThreading SystemThreadingTasks SystemLinq 和 SystemCollectionsConcurrent這些名字空間中提供通過這些新的類型和類開發人員將無需面對如今復雜的多線程開發模式而可以直接使用NET Framework更加高效簡便地開發支持並行計算的應用程序從而更加充分地利用多核CPU的優勢隨著計算核心或者處理器的增加以提升應用程序的性能
而在NET Framework中Task Parallel Library (TPL)是其Parallel Extensions中一個重要組成部分它提供了一種簡便的多線程開發方式通過它所提供的類或者函數可以讓程序員輕松地實現並行計算其中最簡單的就是它的Parallel類
Parallel類
Parallel類就是TPL中的一個用於支持並行計算的類Parallel類提供了諸多的靜態函數只需要簡單的函數調用我們就可以對常用的for循環foreach循環進行並行化下面我們通過一些實際的例子來看看如何利用這個類將我們的應用程序並行化以吃上多核這免費的午餐
創建示例項目
為了演示如何將一個現有的項目並行化我們需要先創建一個示例項目在這個項目中我們將模擬對數據的串行操作然後介紹如何利用Parallel類將對數據的串行操作並行化以充分利用多核CPU的優勢從而提升應用程序的性能
在Visual Studio 中我們新創建一個Visual C#的控制台應用程序然後在這個項目中添加一個類Employee其實現代碼如下
using System;
using SystemCollectionsGeneric;
using SystemLinq;
using SystemText;
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)
{
ConsoleWriteLine(Processing {} employeeEmployeeID);
// 產生一個隨機數
// 用以表示處理當前數據需要的時間
var rand = new Random(DateTimeNowMillisecond);
var delay = randNext( );
var count = ;
var process = true;
while (process)
{
SystemThreadingThreadSleep();
count++;
if (count >= delay)
process = false;
}
return delay;
}
}
// 職員列表類
// 這是我們需要處理的數據
public class EmployeeList : List
{
public EmployeeList()
{
// 將職員添加到列表中
Add(new Employee { EmployeeID = FirstName = 張 LastName = 三 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 李 LastName = 四 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 王 LastName = 麻子 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 趙 LastName = 匡胤 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 錢 LastName = 進 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 孫 LastName = 俊鵬 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 李 LastName = 明 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 周 LastName = 大勇 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 吳 LastName = 明子 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 鄭 LastName = 邦萬 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 王 LastName = 朝 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 馮 LastName = 瑪麗 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 陳 LastName = 良喬 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 褚 LastName = 春晖 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 衛 LastName = 斯理 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 蔣 LastName = 中正 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 沈 LastName = 洋洋 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 韓 LastName = 斌 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 楊 LastName = 雪 HireDate = DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 朱 LastName = 輝 HireDate = DateTimeParse(//) });
DateTimeParse(//) });
Add(new Employee { EmployeeID = FirstName = 朱 LastName = 輝 HireDate = DateTimeParse(//) });
}
}
}
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/11645.html