Parallel 类

定义

提供对并行循环和区域的支持。Provides support for parallel loops and regions.

public ref class Parallel abstract sealed
public static class Parallel
type Parallel = class
Public Class Parallel
继承
Parallel

示例

此示例演示了使用多个语言构造实现并行循环的几种方法。This example demonstrates several approaches to implementing a parallel loop using multiple language constructs.

using System.Threading.Tasks;   
class Test
{
    static int N = 1000;

    static void TestMethod()
    {
        // Using a named method.
        Parallel.For(0, N, Method2);

        // Using an anonymous method.
        Parallel.For(0, N, delegate(int i)
        {
            // Do Work.
        });

        // Using a lambda expression.
        Parallel.For(0, N, i =>
        {
            // Do Work.
        });
    }

    static void Method2(int i)
    {
        // Do work.
    }
}
Imports System.Threading.Tasks

Module Module1

    Sub Main()
        Dim N As Integer = 1000

        ' Using a named method
        Parallel.For(0, N, AddressOf Method2)

        ' Using a lambda expression.
        Parallel.For(0, N, Sub(i)
                               ' Do Work
                           End Sub)
    End Sub

    Sub Method2(ByVal i As Integer)
        ' Do work.
    End Sub

End Module

注解

Parallel类为常见操作(如 for 循环、每个循环和执行一组语句)提供了基于库的数据并行替换。The Parallel class provides library-based data parallel replacements for common operations such as for loops, for each loops, and execution of a set of statements.

方法

For(Int32, Int32, Action<Int32,ParallelLoopState>)

执行 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a for loop in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

For(Int32, Int32, Action<Int32>)

执行 for 循环,其中可能会并行运行迭代。Executes a for loop in which iterations may run in parallel.

For(Int32, Int32, ParallelOptions, Action<Int32,ParallelLoopState>)

执行 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a for loop in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

For(Int32, Int32, ParallelOptions, Action<Int32>)

执行 for 循环,其中可能会并行运行迭代,而且可以配置循环选项。Executes a for loop in which iterations may run in parallel and loop options can be configured.

For(Int64, Int64, Action<Int64,ParallelLoopState>)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a for loop with 64-bit indexes in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

For(Int64, Int64, Action<Int64>)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a for loop with 64-bit indexes in which iterations may run in parallel.

For(Int64, Int64, ParallelOptions, Action<Int64,ParallelLoopState>)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a for loop with 64-bit indexes in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

For(Int64, Int64, ParallelOptions, Action<Int64>)

执行具有 64 位索引的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项。Executes a for loop with 64-bit indexes in which iterations may run in parallel and loop options can be configured.

For<TLocal>(Int32, Int32, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a for loop with thread-local data in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

For<TLocal>(Int32, Int32, ParallelOptions, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a for loop with thread-local data in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

For<TLocal>(Int64, Int64, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有 64 位索引和线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a for loop with 64-bit indexes and thread-local data in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

For<TLocal>(Int64, Int64, ParallelOptions, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有 64 位索引和线程本地数据的 for 循环,其中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a for loop with 64-bit indexes and thread-local data in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with thread-local data on an IEnumerable in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with thread-local data on an IEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据和 64 位索引的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with thread-local data and 64-bit indexes on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with thread-local data on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner<TSource> 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with thread-local data on a OrderablePartitioner<TSource> in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

执行具有 64 位索引和线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner<TSource> 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with 64-bit indexes and with thread-local data on a OrderablePartitioner<TSource> in which iterations may run in parallel , loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(Partitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 上可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with thread-local data on a Partitioner in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(Partitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

执行具有线程本地数据的 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with thread-local data on a Partitioner in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState,Int64>)

执行具有 64 位索引的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with 64-bit indexes on an IEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, Action<TSource>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel.

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)

执行具有 64 位索引的 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 上可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation with 64-bit indexes on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 IEnumerable 中可能会并行运行迭代,而且可以匹配配置循环选项。Executes a foreach (For Each in Visual Basic) operation on an IEnumerable in which iterations may run in parallel and loop options can be configured.

ForEach<TSource>(OrderablePartitioner<TSource>, Action<TSource,ParallelLoopState,Int64>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner<TSource> 中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation on a OrderablePartitioner<TSource> in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

ForEach<TSource>(OrderablePartitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 OrderablePartitioner<TSource> 中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation on a OrderablePartitioner<TSource> in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(Partitioner<TSource>, Action<TSource,ParallelLoopState>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 中可能会并行运行迭代,而且可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(Partitioner<TSource>, Action<TSource>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 上可能会并行运行迭代。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel.

ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 中可能会并行运行迭代,而且可以配置循环选项,可以监视和操作循环的状态。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource>)

执行 foreach(在 Visual Basic 中为 For Each)操作,其中在 Partitioner 中可能会并行运行迭代,而且可以配置循环选项。Executes a foreach (For Each in Visual Basic) operation on a Partitioner in which iterations may run in parallel and loop options can be configured.

Invoke(Action[])

尽可能并行执行提供的每个操作。Executes each of the provided actions, possibly in parallel.

Invoke(ParallelOptions, Action[])

执行所提供的每个操作,而且尽可能并行运行,除非用户取消了操作。Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.

适用于

线程安全性

的所有公共和受保护的成员 Parallel 都是线程安全的,可从多个线程并发使用。All public and protected members of Parallel are thread-safe and may be used concurrently from multiple threads.

另请参阅