在.NET 4多线程的改进 [英] Multithreading improvements in .NET 4

查看:101
本文介绍了在.NET 4多线程的改进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经听说了.NET 4支球队增添了新类的框架,使与线程更好,更容易的工作。

I have heard that the .NET 4 team has added new classes in the framework that make working with threads better and easier.

基本上的问题是什么是新的方式来运行在.NET 4中添加和使用什么是他们专为多线程任务?

Basically the question is what are the new ways to run multithreaded tasks added in .NET 4 and what are they designed to be used for?

UPD:只是要清楚,我不是在寻找的在.NET 4中运行并行任务的一个的办法,我想找出哪些是在新的增加,有可能的话什么情况下会的他们中的每的是最适合的。

UPD: Just to make it clear, I'm not looking for a single way of running parallel tasks in .NET 4, I want to find out which are the new ones added, and if possible what situation would each of them be best suited for..

推荐答案

由于缺乏反应,我决定评估下面与我所学到的答案.. 作为@Scott指出,.NET 4中添加了任务并行库添加了大量的创新,新的方法和途径并行性。

With the lack of responses, I decided to evaluate on the answers below with that I've learned.. As @Scott stated, .NET 4 added the Task Parallel Library which adds a number of innovations, new methods and approaches to parallelism.

  • 一个提到的第一件事就是的Parallel.For Parallel.ForEach 的方法,这让开发人员来处理多个线程多个项目。在这种情况下,框架将决定有多少线程是必要的,并且当创建新线程,而当不通过。
    这是一个非常简单和直接的方式并行存在code,并添加了一些性能提升。
  • 的另一种方式,有点类似于previous办法是使用PLINQ增量剂。他们利用现有的枚举,并与并行LINQ扩展扩展它。所以,如果你有一个现有的LINQ查询,您可以轻松地将其转换为PLINQ。这意味着在PLINQ枚举也将充分利用多线程的,和过滤你使用。凡条款,例如对象列表中的所有操作,将运行多线程吧!
  • 一个在TPL更大的创新是新的工作类。在某些方面,它可能看起来像已经广为人知类,但它需要新的线程池的.NET 4(已经改善了很多优势,相比于previous版本),并且比普通的类等等功能。例如,你可以链任务,其中在链中间任务将仅在previous那些完成启动。示例和深入的解释在<一个截屏href="http://channel9.msdn.com/posts/philpenn/Task-and-TaskTResult-Waiting-and-Continuations/">Channel 9
  • 要加强与任务类的工作,我们可以使用 BlockingCollection&LT;&GT; 。这工作完全在你有一个生产者 - 消费者方案的情况。可以具有产生某些对象,这将随后被消耗和由消费者的方法加工的多个线程。这可以容易地并行化和控制与工作工厂和阻塞集合。从<一个例子很有用截屏href="http://channel9.msdn.com/posts/philpenn/BlockingCollectionT-Demonstration-in-Producer-Consumer-Scenarios/">Channel 9
    这些也可以使用不同的后备存储类(ConcurrentQueue,ConcurentStack,ConcurrentBag),里面全是线程安全的,并在单元排序和性能方面都有不同。示例,其中解释了不同的视频这里
  • 已增加(这可能不是第三方物流的一部分,但可以帮助我们在这里呢)还有一个新的东西是 CountdownEvent 类,它可以帮助我们在任务协调方案(C)。基本上可以让我们等待,直到所有并行任务的完成。截屏与频道9 例如使用
  • One of the first things to mention is the Parallel.For and Parallel.ForEach methods, which allow the developer to process multiple items in multiple threads. The Framework in this case will decide how many threads are necessary, and when to create new threads, and when not to.
    This is a very simple and straightforward way to parallelize existing code, and add some performance boost.
  • Another way, somewhat similar to the previous approaches is using the PLINQ extenders. They take an existing enumeration, and extend it with parallel linq extenders. So if you have an existing linq query, you can easily convert it to PLINQ. What this means is all the operations on the PLINQ enumerable will also take advantage of multiple threads, and filtering your list of objects using a .Where clause, for example, will run in multiple threads now!
  • One of the bigger innovations in the TPL is the new Task class. In some ways it may look like the already well known Thread class, but it takes advantage of the new Thread Pool in .NET 4 (which has been improved a lot compared on previous versions), and is much more functional than the regular Thread class. For example you can chain Tasks where tasks in the middle of the chain will only start when the previous ones finish. Examples and in-depth explanation in a screencast on Channel 9
  • To enhance the work with Task classes, we can use the BlockingCollection<>. This works perfectly in situations where you have a producer-consumer scenario. You can have multiple threads producing some objects, that will then be consumed and processed by consumer methods. This can be easily parallelised and controlled with the Task factory and the blocking collection. Useful screencast with examples from Channel 9
    These can also use different backing storage classes (ConcurrentQueue, ConcurentStack, ConcurrentBag), which are all thread safe, and are different in terms of element ordering and performance. Examples and explanations of them in a different video here
  • One more new thing that has been added (which probably isn't part of the TPL, but helps us here anyway) is the CountdownEvent class, which can help us in "task coordination scenarios" (c). Basically allows us to wait until all parallel tasks are finished. Screencast with example usage on Channel 9

您可以看到许多第9频道截屏和视频被标记为并行计算

You can see a number of screencasts and videos on Channel 9 that are tagged with "Parallel Computing"

这篇关于在.NET 4多线程的改进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆