.NET 4中并行编程的简单例子
2010-04-16 07:09
519 查看
该文可参考http://msdn.microsoft.com/en-us/library/dd460720(VS.100).aspx
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
namespaceForEachDemo { usingSystem; usingSystem.Drawing;//requiressystem.Drawing.dll usingSystem.IO; usingSystem.Threading; usingSystem.Threading.Tasks; classSimpleForEach { staticvoidMain() { //Asimplesourcefordemonstrationpurposes.Modifythispathasnecessary. string[]files=System.IO.Directory.GetFiles(@"C:\Users\Public\Pictures\SamplePictures","*.jpg"); stringnewDir=@"C:\Users\Public\Pictures\SamplePictures\Modified"; System.IO.Directory.CreateDirectory(newDir); //Methodsignature:Parallel.ForEach(IEnumerable<TSource>source,Action<TSource>body) Parallel.ForEach(files,currentFile=> { //Themorecomputationalworkyoudohere,thegreater //thespeedupcomparedtoasequentialforeachloop. stringfilename=System.IO.Path.GetFileName(currentFile); System.Drawing.Bitmapbitmap=newSystem.Drawing.Bitmap(currentFile); bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate180FlipNone); bitmap.Save(System.IO.Path.Combine(newDir,filename)); //Peekbehindthescenestoseehowworkisparallelized. //Butbeaware:ThreadcontentionfortheConsoleslowsdownparallelloops!!! Console.WriteLine("Processing{0}onthread{1}",filename, Thread.CurrentThread.ManagedThreadId); }//closelambdaexpression );//closemethodinvocation //Keeptheconsolewindowopenindebugmode. Console.WriteLine("Processingcomplete.Pressanykeytoexit."); Console.ReadKey(); } } } 注意:
并行编程与之前的多线程的区别,并行编程其实可以理解为多核编程,它可以更好地利用多CPU的优势。而之前的多线程其实是需要切换线程的。
.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}
相关文章推荐
- .NET 4中并行编程的简单例子
- 一个简单的.NET的类的例子:华氏和摄氏,公里和英里的转换类的例子
- .NET(C#) Internals: 以一个数组填充的例子初步了解.NET 4.0中的并行(一)
- .NET 并行(多核)编程系列之五 Task执行和异常处理
- .net 4.0 中的特性总结(五):并行编程
- .NET 4 并行(多核)编程系列之二 从Task开始
- .NET 4 并行(多核)编程系列之二 从Task开始
- 一个时间编程简单例子
- 关于使用PRO*C编程的一些简单说明和例子
- 关于使用PRO*C编程的一些简单说明和例子
- 关于Java的RMI编程的一个简单的例子
- Loadrunner中 C 语言编程 简单例子
- Socket 同步编程的一个简单例子,适合初学者
- OOP编程iBatis 学习笔记之- 环境配置及简单例子测试
- .NET(C#) Internals: 以一个数组填充的例子初步了解.NET 4.0中的并行(一)
- Linux内核协议栈(2) 由简单的socket编程例子开始
- 简单的windows服务编程框架(MSDN例子代码)
- 【Cuda并行编程之一】二分查找的探究以及Cuda的简单实现&&相关面试题介绍
- .Net中的socket编程例子
- 3个学习Socket编程的简单例子:TCP Server/Client, Select