Danh mục

Visual C# 2010 Recipes solution_7

Số trang: 95      Loại file: pdf      Dung lượng: 1.91 MB      Lượt xem: 12      Lượt tải: 0    
10.10.2023

Phí tải xuống: 39,000 VND Tải xuống file đầy đủ (95 trang) 0
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Tham khảo tài liệu visual c# 2010 recipes solution_7, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Visual C# 2010 Recipes solution_7 CHAPTER 15 ■ PARALLEL PROGRAMMINGrequested became available—this example waits for all of the tasks to complete before obtaining theresults.using System;using System.Threading;using System.Threading.Tasks;namespace Recipe15_03{ class Recipe15_03 { static void Main(string[] args) { Console.WriteLine(Press enter to start); Console.ReadLine(); // Create the tasks. Task task1 = Task.Factory.StartNew(() => writeDays()); Task task2 = Task.Factory.StartNew(() => writeMonths()); Task task3 = Task.Factory.StartNew(() => writeCities()); // Wait for all of the tasks to complete. Task.WaitAll(task1, task2, task3); // Get the results and write them out. Console.WriteLine({0} days were written, task1.Result); Console.WriteLine({0} months were written, task2.Result); Console.WriteLine({0} cities were written, task3.Result); // Wait to continue. Console.WriteLine( Main method complete. Press Enter); Console.ReadLine(); } static int writeDays() { string[] daysArray = { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; foreach (string day in daysArray) { Console.WriteLine(Day of the Week: {0}, day); Thread.Sleep(500); } return daysArray.Length; } 735 CHAPTER 15 ■ PARALLEL PROGRAMMING static int writeMonths() { string[] monthsArray = { Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; foreach (string month in monthsArray) { Console.WriteLine(Month: {0}, month); Thread.Sleep(500); } return monthsArray.Length; } static int writeCities() { string[] citiesArray = { London, New York, Paris, Tokyo, Sydney }; foreach (string city in citiesArray) { Console.WriteLine(City: {0}, city); Thread.Sleep(500); } return citiesArray.Length; } } } 15-4. Parallel Process a Collection Problem You need to parallel process each element in a collection. Solution Use the System.Threading.Parallel.ForEach method to create a new task to process each of the elements in a collection. Optionally, use System.Threading.ParallelOptions to limit the degree of parallelism that will be used. How It Works The static Parallel.ForEach method accepts a collection, a function delegate, and an optional instance of ParallelOptions as arguments. A new task is created to process each element in the collection using the function referenced by the delegate. The number of concurrent tasks is controlled by the ParallelOptions.MaxDegreeOfParallelism property—a value of -1 means that the degree of parallelism736 CHAPTER 15 ■ PARALLEL PROGRAMMINGwill be determined by the runtime, whereas a value of 1 or more limits the number of tasks that will runat the same time (a value of 0 will throw an exception).The CodeThe following example creates tasks to process each element of a simple array using the printNumbersmethod. We have called Thread.Sleep in this method to slow down the processing so that the example isclearer. We use the MaxDegreeOfParallelism property of ParallelOptions to ensure that at most twotasks are performed simultaneously—when running the example, notice that the output from the firsttwo tasks is intermingled and then followed by the output from the third task.using System;using System.Threading;using System.Threading.Tasks;namespace Recipe15_04{ class Recipe15_04 { static void Main(string[] args) { Console.WriteLine(Press enter to start); Console.ReadLine(); // Define the data we want to process. int[] numbersArray = { 100, 200, 300 }; // Configure the options. ParallelOptions options = new ParallelOptions(); options ...

Tài liệu được xem nhiều: