任务并行库 (TPL)Task Parallel Library (TPL)

任务并行库 (TPL) 是 System.ThreadingSystem.Threading.Tasks 空间中的一组公共类型和 API。The Task Parallel Library (TPL) is a set of public types and APIs in the System.Threading and System.Threading.Tasks namespaces. TPL 的目的是通过简化将并行和并发添加到应用程序的过程来提高开发人员的工作效率。The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. TPL 动态缩放并发的程度以最有效地使用所有可用的处理器。The TPL scales the degree of concurrency dynamically to most efficiently use all the processors that are available. 此外,TPL 还处理工作分区、ThreadPool 上的线程调度、取消支持、状态管理以及其他低级别的细节操作。In addition, the TPL handles the partitioning of the work, the scheduling of threads on the ThreadPool, cancellation support, state management, and other low-level details. 通过使用 TPL,你可以在将精力集中于程序要完成的工作,同时最大程度地提高代码的性能。By using TPL, you can maximize the performance of your code while focusing on the work that your program is designed to accomplish.

自 .NET Framework 4 起,首选 TPL 编写多线程代码和并行代码。Starting with the .NET Framework 4, the TPL is the preferred way to write multithreaded and parallel code. 但是,并不是所有代码都适合并行化;例如,如果某个循环在每次迭代时只执行少量工作,或它在很多次迭代时都不运行,那么并行化的开销可能导致代码运行更慢。However, not all code is suitable for parallelization; for example, if a loop performs only a small amount of work on each iteration, or it doesn't run for many iterations, then the overhead of parallelization can cause the code to run more slowly. 此外,像任何多线程代码一样,并行化会增加程序执行的复杂性。Furthermore, parallelization like any multithreaded code adds complexity to your program execution. 尽管 TPL 简化了多线程方案,但我们建议你对线程处理概念(例如,锁、死锁和争用条件)进行基本的了解,以便能够有效地使用 TPL。Although the TPL simplifies multithreaded scenarios, we recommend that you have a basic understanding of threading concepts, for example, locks, deadlocks, and race conditions, so that you can use the TPL effectively.

标题Title 说明Description
数据并行Data Parallelism 描述如何创建并行的 forforeach 循环(在 Visual Basic 中为 ForFor Each)。Describes how to create parallel for and foreach loops (For and For Each in Visual Basic).
基于任务的异步编程Task-based Asynchronous Programming 描述如何通过使用 Parallel.Invoke 隐式创建和运行任务,或通过直接使用 Task 对象显式创建和运行任务。Describes how to create and run tasks implicitly by using Parallel.Invoke or explicitly by using Task objects directly.
数据流Dataflow 描述如何使用 TPL 数据流库中的数据流组件处理多项运算,这些运算必须彼此通信,或在数据可用时处理数据。Describes how to use the dataflow components in the TPL Dataflow Library to handle multiple operations that must communicate with one another or to process data as it becomes available.
将 TPL 和其他异步模式结合使用Using TPL with Other Asynchronous Patterns 描述如何将 TPL 与 .NET 中的其他异步模式一起使用。Describes how to use TPL with other asynchronous patterns in .NET
数据和任务并行的潜在问题Potential Pitfalls in Data and Task Parallelism 描述一些常见缺陷以及如何避免它们。Describes some common pitfalls and how to avoid them.
并行 LINQ (PLINQ)Parallel LINQ (PLINQ) 描述如何使用 LINQ 查询实现数据并行化。Describes how to achieve data parallelism with LINQ queries.
并行编程Parallel Programming .NET 并行编程的顶级节点。Top level node for .NET parallel programming.

另请参阅See also