工作平行程式庫 (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.
使用具有其他非同步模式的 TPLUsing 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