tailieunhanh - C# in Depth what you need to master c2 and 3 phần 10

Tất cả các nhà cung cấp LINQ chúng tôi đã nhìn thấy cho đến nay đã hành động trên một nguồn dữ liệu cụ thể nào đó, và thực hiện sự chuyển đổi thích hợp. Chủ đề tiếp theo của chúng tôi là hơi khác nhau nhưng nó là một trong những Tôi đặc biệt vui mừng về. | LINQ beyond .NET 349 All of the LINQ providers we ve seen so far have acted on a particular data source and performed the appropriate transformations. Our next topic is slightly different but it s one I m particularly excited about. PARALLEL LINQ PLINQ Ten years ago the idea of even fairly low-to-middling laptops having dual-processor cores would have seemed ridiculous. Today that s taken for granted and if the chip manufacturers plans are anything to go by that s only the start. Of course it s only useful to have more than one processor core if you ve got tasks you can run in parallel. ParallelLINQ or PLINQ for short is a project with one simple goal to execute LINQ to Objects queries in parallel realizing the benefits of multithreading with as few headaches as possible. At the time of this writing PLINQ is targeted to be released as part of Parallel Extensions the next generation of .NET concurrency support. The sample I describe is based on the December 2007 Community Technology Preview CTP . Using PLINQ is simple if and only if you have to perform the same task on each element in a sequence and those tasks are independent. If you need the result of one calculation step in order to find the next PLINQ is not for you but many CPUintensive tasks can in fact be done in parallel. To tell the compiler to use PLINQ you just need to call AsParallel an extension method on IEnumerable T on your data source and let PLINQ handle the threading. As with IQueryable the magic is just normal compiler method resolution AsParallel returns an IParallelEnumerable and the ParallelEnumerable class provides static methods to handle the standard query operators. Listing demonstrates PLINQ in an entirely artificial way putting threads to sleep for random periods instead of actually hitting the processor hard. Listing Executing a LINQ query on multiple threads with Parallel LINQ static int ObtainLengthSlowly string name 10000 return .

TỪ KHÓA LIÊN QUAN