parallel.foreach,parallelforeach运行结束

http://www.itjxue.com  2023-01-23 05:15  来源:未知  点击次数: 

何时使用 Parallel.ForEach,何时使用 PLINQ

foreach和for循环类似的,使用foreach的好处是,当遍历数组时,因为有些语言的起始位置是从0开始的,而有些语言的起始位置却使从1开始,当语言支持foreach语法时,使用了foreach,它就自动帮你从数组第一位开始遍历,而不用你管第一位的下标是0还是1。而使用for来遍历的话,你还要确定该语言对数组的定义是下标1开始还是下标0开始。

C# 带泛型的Parallel.For和Foreach怎么用?

using?System;

using?System.Collections;

using?System.Collections.Generic;

class?Program

{

????static?void?Main(string[]?args)

????{

????????Student?s;

????????LinkedListStudent?list?=?new?LinkedListStudent();

????????LinkedListNodeStudent?node;

????????s?=?new?Student(12);

????????node?=?new?LinkedListNodeStudent(s);

????????list.AddLast(node);

????????s?=?new?Student(23);

????????node?=?new?LinkedListNodeStudent(s);

????????list.AddLast(node);

????????s?=?new?Student(34);

????????node?=?new?LinkedListNodeStudent(s);

????????list.AddLast(node);

????????s?=?new?Student(45);

????????node?=?new?LinkedListNodeStudent(s);

????????list.AddLast(node);

C#中原来串行代码为 foreach (DataRow row in ResultTable.Rows)请问如何改为Parallel.ForEach并行写法?

应该是

Parallel.ForEach(

ResultTable.Rows,

(row) ={

//处理语句,比如

Console.WriteLine(row.ToString());

}

);

请问如何:编写简单的 Parallel.ForEach 循环

示例 Visual Basic ' How to: Write a Simple Parallel.ForEach Loop ' IMPORTANT!!!: Add reference to System.Drawing.dll Imports System.Threading Imports System.Threading.Tasks Imports System.Drawing Module ForEachDemo Sub Main() ' A simple source for demonstration purposes. Modify this path as necessary. Dim files AsString() = System.IO.Directory.GetFiles("C:\Users\Public\Pictures\Sample Pictures", "*.jpg") Dim newDir AsString = "C:\Users\Public\Pictures\Sample Pictures\Modified" System.IO.Directory.CreateDirectory(newDir) ' Method signature: Parallel.ForEach(IEnumerableTSource source, ActionTSource body) ' Be sure to add a reference to System.Drawing.dll. Parallel.ForEach(files, Sub(currentFile) ' The more computational work you do here, the greater ' the speedup compared to a sequential foreach loop. Dim filename AsString = System.IO.Path.GetFileName(currentFile) Dim bitmap AsNew System.Drawing.Bitmap(currentFile) bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone) bitmap.Save(System.IO.Path.Combine(newDir, filename)) ' Peek behind the scenes to see how work is parallelized. ' But be aware: Thread contention for the Console slows down parallel loops!!! Console.WriteLine("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId) 'close lambda expression and method invocation EndSub) ' Keep the console window open in debug mode. Console.WriteLine("Processing complete. Press any key to exit.") Console.ReadKey() EndSubEndModule C# namespace ForEachDemo { using System; using System.Drawing; // requires system.Drawing.dll using System.IO; using System.Threading; using System.Threading.Tasks; class SimpleForEach { staticvoid Main() { // A simple source for demonstration purposes. Modify this path as necessary.string[] files = System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\Sample Pictures", "*.jpg"); string newDir = @"C:\Users\Public\Pictures\Sample Pictures\Modified"; System.IO.Directory.CreateDirectory(newDir); // Method signature: Parallel.ForEach(IEnumerableTSource source, ActionTSource body) Parallel.ForEach(files, currentFile = { // The more computational work you do here, the greater // the speedup compared to a sequential foreach loop.string filename = System.IO.Path.GetFileName(currentFile); System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(currentFile); bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); bitmap.Save(System.IO.Path.Combine(newDir, filename)); // Peek behind the scenes to see how work is parallelized.// But be aware: Thread contention for the Console slows down parallel loops!!! Console.WriteLine("Processing {0} on thread {1}", filename, Thread.CurrentThread.ManagedThreadId); } //close lambda expression ); //close method invocation // Keep the console window open in debug mode. Console.WriteLine("Processing complete. Press any key to exit."); Console.ReadKey(); } } } ForEach 循环的工作方式类似于 For 循环。根据系统环境,对源集合进行分区,并在多个线程上计划工作。系统中的处理器越多,并行方法的运行速度越快。对于某些源集合,顺序循环可能更快,具体取决于源的大小和正在执行的工作类型。有关性能的更多信息,请参见数据并行和任务并行中的潜在缺陷有关并行循环的更多信息,请参见如何:编写简单的 Parallel.For 循环若要将 ForEach 用于非泛型集合,可以使用 Cast(Of (TResult)) 扩展方法将集合转换为泛型集合,如下面的示例所示: Visual Basic Parallel.ForEach(nonGenericCollection.Cast(OfObject), _ Sub(currentElement) ' ... work with currentElement EndSub) C# Parallel.ForEach(nonGenericCollection.Castobject(), currentElement = { }); 还可以使用并行 LINQ (PLINQ) 来并行处理 IEnumerable(Of (T)) 数据源。PLINQ 使您可以使用声明性的查询语法表达循环行为。

(责任编辑:IT教学网)

更多

相关网页文字特效文章

推荐网页文字特效文章