.net 如何使用Task设置最大并发线程数

g6ll5ycj  于 2023-02-01  发布在  .NET
关注(0)|答案(3)|浏览(308)

我正在写一个压力测试实用程序。在这个实用程序中,我希望我连续地加载10个线程(10,000个线程中)。下面是我的代码

Stopwatch watch = new Stopwatch();
        watch.Start();

        int itemProcessed = 0;

        do
        {
            List<Task> taskList = new List<Task>();
            for (int i = 0; i < _parallelThreadCount; i++)
            {
                taskList.Add(Task.Factory.StartNew(() => _taskDelegate()));
                itemProcessed++;
            }
            Task.WaitAll(taskList.ToArray());
        } while (itemProcessed < _batchSize);

        watch.Stop();

现在的问题是,我使用了Task.WaitAll,因此初始负载是10个线程,然后是9、8、7、6、5、4、3、2、1、0。然后我又添加了10个线程。
谁能给予我一个主意,如何实现这一点。

ctehm74n

ctehm74n1#

Shaaman的答案很好,可能是你想在你的特定场景中使用的答案。我只是提出了一些你可以使用的其他可能的选择,这些选择可能更适用于其他场景。
My blog post展示了如何使用Tasks和Actions来实现这一点,并提供了一个示例项目,您可以下载并运行该项目来查看两者的运行情况。

采取行动

如果使用Actions,你可以使用内置的.Net Parallel.invoke函数。这里我们限制它最多并行运行10个线程。

var listOfActions = new List<Action>();
for (int i = 0; i < 10000; i++)
{
    // Note that we create the Action here, but do not start it.
    listOfActions.Add(() => DoSomething());
}

var options = new ParallelOptions {MaxDegreeOfParallelism = 10};
Parallel.Invoke(options, listOfActions.ToArray());

有任务

Tasks没有内置函数,但是你可以使用我在博客上提供的函数。

/// <summary>
    /// Starts the given tasks and waits for them to complete. This will run, at most, the specified number of tasks in parallel.
    /// <para>NOTE: If one of the given tasks has already been started, an exception will be thrown.</para>
    /// </summary>
    /// <param name="tasksToRun">The tasks to run.</param>
    /// <param name="maxTasksToRunInParallel">The maximum number of tasks to run in parallel.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    public static void StartAndWaitAllThrottled(IEnumerable<Task> tasksToRun, int maxTasksToRunInParallel, CancellationToken cancellationToken = new CancellationToken())
    {
        StartAndWaitAllThrottled(tasksToRun, maxTasksToRunInParallel, -1, cancellationToken);
    }

    /// <summary>
    /// Starts the given tasks and waits for them to complete. This will run, at most, the specified number of tasks in parallel.
    /// <para>NOTE: If one of the given tasks has already been started, an exception will be thrown.</para>
    /// </summary>
    /// <param name="tasksToRun">The tasks to run.</param>
    /// <param name="maxTasksToRunInParallel">The maximum number of tasks to run in parallel.</param>
    /// <param name="timeoutInMilliseconds">The maximum milliseconds we should allow the max tasks to run in parallel before allowing another task to start. Specify -1 to wait indefinitely.</param>
    /// <param name="cancellationToken">The cancellation token.</param>
    public static void StartAndWaitAllThrottled(IEnumerable<Task> tasksToRun, int maxTasksToRunInParallel, int timeoutInMilliseconds, CancellationToken cancellationToken = new CancellationToken())
    {
        // Convert to a list of tasks so that we don&#39;t enumerate over it multiple times needlessly.
        var tasks = tasksToRun.ToList();

        using (var throttler = new SemaphoreSlim(maxTasksToRunInParallel))
        {
            var postTaskTasks = new List<Task>();

            // Have each task notify the throttler when it completes so that it decrements the number of tasks currently running.
            tasks.ForEach(t => postTaskTasks.Add(t.ContinueWith(tsk => throttler.Release())));

            // Start running each task.
            foreach (var task in tasks)
            {
                // Increment the number of tasks currently running and wait if too many are running.
                throttler.Wait(timeoutInMilliseconds, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();
                task.Start();
            }

            // Wait for all of the provided tasks to complete.
            // We wait on the list of "post" tasks instead of the original tasks, otherwise there is a potential race condition where the throttler&#39;s using block is exited before some Tasks have had their "post" action completed, which references the throttler, resulting in an exception due to accessing a disposed object.
            Task.WaitAll(postTaskTasks.ToArray(), cancellationToken);
        }
    }

然后创建任务列表并调用函数来运行它们,比如说一次最多同时运行10个任务,您可以这样做:

var listOfTasks = new List<Task>();
for (int i = 0; i < 10000; i++)
{
    var count = i;
    // Note that we create the Task here, but do not start it.
    listOfTasks.Add(new Task(() => Something()));
}
Tasks.StartAndWaitAllThrottled(listOfTasks, 10);
cu6pst1q

cu6pst1q2#

如果您可以稍微调整一下代码结构(阅读:替换您的do while循环),您可以使用Parallel class

List<int> data = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Parallel.ForEach(data, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, d =>
{
    Console.WriteLine(d);
});

您可能最感兴趣的是ParallelOptionsMaxDegreeOfParallelism属性-它指定可以同时运行多少个线程。
编辑:
由于您没有任务列表,而只是想多次重复相同的操作,因此可以使用Parallel.For

int repeatCount = 100;
int itemProcessed = 0;
Parallel.For(0, repeatCount, new ParallelOptions() { MaxDegreeOfParallelism = 10 }, i =>
{
    _taskDelegate();
    System.Threading.Interlocked.Increment(ref itemProcessed);
});

请注意,如果使用itemProcessed的唯一原因是检查循环要工作多长时间,则可以安全地从上面的代码中删除这两行代码。

ego6inou

ego6inou3#

这个解决方案松散地基于@deadlydog answer,但是适用于那些你想要运行async/await函数的情况,执行的顺序不一定(这不是一个队列),但是所有的任务都应该运行。

var items = GetRandomNumbers(10, 3000, 10000).ToArray();
var tasks = new List<Task>();
using var throttler = new SemaphoreSlim(3);

for (var i = 0; i < items.Length; i++)
{
    var nbr = i;
    tasks.Add(Task.Run(async () =>
    {
        await throttler.WaitAsync(-1, CancellationToken.None);
        try
        {
            // Your code here
            var item = items[nbr];
            Console.WriteLine($"Starting task {nbr}. Delay {item}ms.");
            await Task.Delay(item);
            Console.WriteLine($"Completed task {nbr}. Delay {item}ms.");
        }
        finally
        {
            throttler.Release();
        }
    }));
}

await Task.WhenAll(tasks.ToArray());

static IEnumerable<int> GetRandomNumbers(int count, int min = 0, int max = int.MaxValue)
{
    var rand = new Random();
    var values = new List<int>();

    for (var i = 0; i < count; ++i)
        values.Add(rand.Next(min, max));

    return values;
}

相关问题