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
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 ...
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ìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 317 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 305 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 290 0 0 -
70 trang 251 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 238 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 233 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 214 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 208 0 0 -
Tổng hợp 30 lỗi thương gặp cho những bạn mới sử dụng máy tính
9 trang 205 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 204 0 0