C# - 线程池与任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1774670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - ThreadPool vs Tasks
提问by TheAJ
As some may have seen in .NET 4.0, they've added a new namespace System.Threading.Tasks
which basically is what is means, a task. I've only been using it for a few days, from using ThreadPool.
正如一些人可能在 .NET 4.0 中看到的那样,他们添加了一个新的命名空间System.Threading.Tasks
,这基本上就是一个任务。从使用 ThreadPool 开始,我只使用了几天。
Which one is more efficient and less resource consuming? (Or just better overall?)
哪一种更高效且资源消耗更少?(或者只是整体更好?)
采纳答案by Jeremy McGee
The objective of the Tasks namespace is to provide a pluggable architecture to make multi-tasking applications easier to write and more flexible.
Tasks 命名空间的目标是提供一个可插拔的架构,使多任务应用程序更容易编写和更灵活。
The implementation uses a TaskScheduler
object to control the handling of tasks. This has virtual methods that you can override to create your own task handling. Methods include for instance
该实现使用一个TaskScheduler
对象来控制任务的处理。它具有虚拟方法,您可以覆盖这些方法来创建自己的任务处理。方法包括例如
protected virtual void QueueTask(Task task)
public virtual int MaximumConcurrencyLevel
There will be a tiny overhead to using the default implementation as there's a wrapper around the .NET threads implementation, but I'd not expect it to be huge.
使用默认实现会产生很小的开销,因为 .NET 线程实现有一个包装器,但我不认为它会很大。
There is a (draft) implementation of a custom TaskScheduler that implements multiple tasks on a single thread here.
有一个(草案)实现自定义的TaskScheduler的是在单个线程实现了多个任务在这里。
回答by Henk Holterman
which one is more efficient and less resource consuming?
哪一种更高效且资源消耗更少?
Irrelevant, there will be very little difference.
无关紧要,差别很小。
(Or just better overall)
(或者只是整体更好)
The Task class will be the easier-to-use as it offers a very clean interface for starting and joining threads, and transfers exceptions. It also supports a (limited) form of load balancing.
Task 类将更易于使用,因为它提供了一个非常干净的接口来启动和加入线程,并传输异常。它还支持(有限)形式的负载平衡。
回答by Prashant Lakhlani
Another good point to consider about task is, when you use ThreadPool, you don't have any way to abort or wait on the running threads (unless you do it manually in the method of thread), but using task it is possible. Please correct me if I'm wrong
关于 task 的另一个需要考虑的一点是,当您使用 ThreadPool 时,您没有任何方法可以中止或等待正在运行的线程(除非您在线程的方法中手动执行),但是使用 task 是可能的。如果我错了,请纠正我
回答by Mickey Perlstein
Scheduling is an important aspect of parallel tasks.
调度是并行任务的一个重要方面。
Unlike threads, new tasks don't necessarily begin executing immediately. Instead, they are placed in a work queue. Tasks run when their associated task scheduler removes them from the queue, usually as cores become available. The task scheduler attempts to optimize overall throughput by controlling the system's degree of concurrency. As long as there are enough tasks and the tasks are sufficiently free of serializing dependencies, the program's performance scales with the number of available cores. In this way, tasks embody the concept of potential parallelism
与线程不同,新任务不一定立即开始执行。相反,它们被放置在工作队列中。当相关的任务调度程序将它们从队列中删除时,任务就会运行,通常是在内核可用时。任务调度器试图通过控制系统的并发程度来优化整体吞吐量。只要有足够多的任务,并且任务足够没有序列化依赖性,程序的性能就会随着可用内核的数量而扩展。这样,任务就体现了潜在并行性的概念
As I saw on msdn http://msdn.microsoft.com/en-us/library/ff963549.aspx
正如我在 msdn http://msdn.microsoft.com/en-us/library/ff963549.aspx上看到的
回答by Ivan I?in
"Starting with the .NET Framework 4, the TPL is the preferred way to write multithreaded and parallel code."
“从 .NET Framework 4 开始,TPL 是编写多线程和并行代码的首选方式。”
回答by fabriciorissetto
Thread
线
The bare metal thing, you probably don't need to use it, you probably can use a LongRunning
Task and benefit from its facilities.
裸机的东西,您可能不需要使用它,您可能可以使用LongRunning
Task 并从其设施中受益。
Tasks
任务
Abstraction above the Threads. It uses the thread pool(unless you specify the task as a LongRunning
operation, if so, a new thread is created under the hood for you).
线程之上的抽象。它使用线程池(除非您将任务指定为LongRunning
操作,如果是这样,则会在后台为您创建一个新线程)。
Thread Pool
线程池
As the name suggests: a pool of threads. Is the .NET framework handling a limited number of threads for you. Why? Because opening 100 threads to execute expensive CPU operations on a CPU with just 8 cores definitely is not a good idea. The framework will maintain this pool for you, reusing the threads (not creating/killing them at each operation), and executing some of they in parallel in a way that your CPU will not burn.
顾名思义:线程池。.NET 框架是否为您处理有限数量的线程。为什么?因为在只有 8 个内核的 CPU 上打开 100 个线程来执行昂贵的 CPU 操作绝对不是一个好主意。该框架将为您维护这个池,重用线程(而不是在每个操作中创建/杀死它们),并以不会消耗 CPU 的方式并行执行其中的一些线程。
OK, but when to use each one?
好的,但是什么时候使用每个呢?
In resume: always use tasks.
在简历中:始终使用任务。
Task is an abstratcion, so it is a lot easier to use. I advise you to always try to use Tasks and if you face some problem that makes you need to handle a thread by yourself (probably 1% of the time) then use threads.
任务是一个抽象,所以它更容易使用。我建议你总是尝试使用 Tasks,如果你遇到一些问题,需要你自己处理一个线程(可能有 1% 的时间),然后使用线程。
BUT be aware that:
但请注意:
- I/O Bound: For I/O bound operations (database calls, read/write files, APIs calls, etc) never use normal tasks, use
LongRunning
tasks or threads if you need to, but not normal tasks. Because it would lead you to a thread pool with a few threads busy and a lot of another tasks waiting for its turn to take the pool. - CPU Bound: For CPU bound operations just use the normal tasks and be happy.
- I/O 绑定:对于 I/O 绑定操作(数据库调用、读/写文件、API 调用等),永远不要使用普通任务,
LongRunning
如果需要,请使用任务或线程,但不要使用普通任务。因为它会导致您进入一个线程池,其中有几个线程很忙,还有很多其他任务在等待轮到它来占用池。 - CPU 绑定:对于 CPU 绑定操作,只需使用正常任务即可。
回答by J. Doe
ThreadPooland Taskdifference is very simple. To understand task you should know about the threadpool.
ThreadPool和Task 的区别很简单。要了解任务,您应该了解线程池。
ThreadPoolis basically help to manage and reuse the free threads. In other words a threadpool is the collection of background thread.
ThreadPool基本上是帮助管理和重用空闲线程。换句话说,线程池是后台线程的集合。
Simple definition of task can be:
任务的简单定义可以是:
Taskwork asynchronously manages the the unit of work. In easy words Task doesn't create new threads. Instead it efficiently manages the threads of a threadpool.Tasks are executed by TaskScheduler, which queues tasks onto threads.
任务工作异步管理工作单元。简而言之,Task 不会创建新线程。相反,它有效地管理线程池的线程。Tasks 由 TaskScheduler 执行,TaskScheduler 将任务排队到线程上。