我正在使用TPL,使用函数Task.Factory.StartNew()
将新任务添加到系统线程池中。唯一的问题是,我正在添加大量线程,我认为它会创建过多的线程,超出了处理器的处理能力。是否有一种方法可以在此线程池中设置最大线程数?
我正在使用TPL,使用函数Task.Factory.StartNew()
将新任务添加到系统线程池中。唯一的问题是,我正在添加大量线程,我认为它会创建过多的线程,超出了处理器的处理能力。是否有一种方法可以在此线程池中设置最大线程数?
默认的TaskScheduler
(从TaskScheduler.Default
获取)是类型为(内部类)ThreadPoolTaskScheduler
。该实现使用ThreadPool
类来排队任务(如果Task
没有使用TaskCreationOptions.LongRunning
创建 - 在这种情况下,为每个任务创建一个新线程)。
因此,如果您想限制通过new Task(() => Console.WriteLine("In task"))
创建的Task
对象可用的线程数,可以像这样限制全局线程池中的可用线程:
// Limit threadpool size
int workerThreads, completionPortThreads;
ThreadPool.GetMaxThreads(out workerThreads, out completionPortThreads);
workerThreads = 32;
ThreadPool.SetMaxThreads(workerThreads, completionPortThreads);
调用 ThreadPool.GetMaxThreads()
是为了避免缩小 completionPortThreads
。
注意,这可能是一个不好的想法 - 因为所有没有指定调度程序的任务和任意数量的其他类都使用默认线程池,设置大小过低可能会引起副作用:饥饿等。
ThreadPool.SetMaxThreads(workerThreads, completionPortThreads);
不再起作用。 - Will Huang如果使用Actions,你可以使用内置的.Net Parallel.Invoke函数。这里我们将它限制在最多同时运行3个线程。
var listOfActions = new List<Action>();
for (int i = 0; i < 10; i++)
{
// Note that we create the Action here, but do not start it.
listOfActions.Add(() => DoSomething());
}
var options = new ParallelOptions {MaxDegreeOfParallelism = 3};
Parallel.Invoke(options, listOfActions.ToArray());
由于您在此处使用任务,因此没有内置函数。但是,您可以使用我在博客上提供的函数。
/// <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'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'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);
}
}
然后创建您的任务列表并调用函数以运行它们,例如每次最多同时运行3个,您可以这样做:
var listOfTasks = new List<Task>();
for (int i = 0; i < 10; i++)
{
var count = i;
// Note that we create the Task here, but do not start it.
listOfTasks.Add(new Task(() => Something()));
}
Tasks.StartAndWaitAllThrottled(listOfTasks, 3);