在不同方式的不同,使并发程序 [英] Differences in the different ways to make concurrent programs

查看:116
本文介绍了在不同方式的不同,使并发程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之间的区别是什么:

  1. 在开始一个新的线程
  2. 使用TPL
  3. 使用的BackgroundWorker

所有这些创造并发性,但这些是什么之间的低层次的区别在哪里?是否所有的3化妆线程呢?

All of these create concurrency but what are the low-level differences between these? Do all 3 make threads anyway?

感谢

推荐答案

他们都使用线程内部,分歧做每个API的抽象级别和线程是如何被利用。让我们再为了你的清单了一下,看看从最低的三种技术,以抽象的最高水平:

They all use threads internally, the differences are to do with the abstraction level of each API and how the threads are utilised. Lets re-order your list a bit and look at the three techniques from lowest to highest levels of abstraction:

  1. 开始一个新的线程手动

这居然会在操作系统的一个新的线程。您的code将在该线程执行。

This actually creates a new thread in the OS. Your code will be executed on that thread.

使用一个BackgroundWorker

在内部使用这个所谓的.NET 线程池。线程池基本上是可用线程池。您的code被分配到可用线程之一,是运行在该线程。

Internally this uses something called the .net ThreadPool. The thread pool is basically a pool of available threads. Your code is assigned onto one of the available threads and is run on that thread.

池管理可用线程数和将在内部创建并在一定范围内根据需要销毁线程。这是有用的,因为池可以有一些算法来优化线程的创建。线程的创建是一项相当昂贵的过程,因此,如果适当的线程池保持线程活着,重用他们将来的请求。您可以通过指定线程最小/最大数字和一些小的调整一样,有一些有限的控制池。

The pool manages the number of available threads and will internally create and destroy threads as required within certain bounds. This is useful because the pool can have some algorithms to optimise thread creation. Thread creation is quite an expensive process, so if appropriate the thread pool keep threads alive and reuse them for future requests. You can have some limited control over the pool by specifying min/max numbers of threads and some minor tweaks like that.

有使用线程池直接,如<一的其他方式href="http://msdn.microsoft.com/en-us/library/system.threading.threadpool.queueuserworkitem.aspx">QueueUserWorkItem(...).

There are other ways of using the ThreadPool directly such as QueueUserWorkItem(...).

使用任务并行库

这是一个更高的抽象。创建任务,并告诉第三方物流来执行它们。该TPL隐藏所有关于到底有多少线程和事情的轻重缓急将用于等TPL有重用线程,并根据具体的机器性能和可用的CPU资源进行管理的能力的担忧。

This is an even higher abstraction. You create "tasks" and tell the TPL to execute them. The TPL hides all the concerns regarding exactly how many threads and what priorities will be used etc. The TPL has the ability to reuse threads and manage them according to specific machine performance and available CPU resources.

例如给定的100个任务,在一个四核的TPL可能产卵4个线程,但在8核心可能酿出8及以上的可用线程分配任务,每个任务完成。

For example given 100 tasks, on a Quad core the TPL might spawn 4 threads, but on an 8 core it might spawn 8 and distribute the tasks over the available threads as each task completes.

因此​​,要回答你的问题。所有3技术使用线程,但你每一级上去控制和认识你的金额超过这些线程被降低。

So to answer you question. All 3 techniques use threads, but as you go up each level the amount of control and awareness you have over those threads is reduced.

在大多数情况下,我会建议你使用第三方物流。除非你在线程的数量和它们的创建/销毁需要特定的非常精确的控制第三方物流将处理这一切非常好你。

In most cases, I would recommend you use the TPL. Unless you need specific very exact control over the number of threads and their creation/destruction the TPL will handle it all very well for you.

这篇关于在不同方式的不同,使并发程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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