熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

VS2010中Parallel類實現並行計算

2022-06-13   來源: .NET編程 

  在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
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.