Да
Похоже, вы действительно можете использовать это для вычисления количества активных потоков пула потоков.
По крайней мере, в .NET Framework 4.6.1
Test
Я написал простенькое консольное приложение, чтобы проверить это, запланировав кучу задач и посмотрев, сколько потоков создано.
Вы можете видеть, что после 4 он ждет некоторое время, прежде чемсоздание дополнительных потоков.
Вывод
---- Start ---- 17:01:55.2583
ThreadPool.GetAvailableThreads: 2,047
ThreadPool.GetMaxThreads: 2,047
ThreadPool.GetMinThreads: 4
ThreadPool.CurrentThreads: 0
---- After Scheduling 1 ---- 17:01:55.2793
ThreadPool.GetAvailableThreads: 2,046
ThreadPool.GetMaxThreads: 2,047
ThreadPool.GetMinThreads: 4
ThreadPool.CurrentThreads: 1
---- After Scheduling 50 ---- 17:01:55.2803
ThreadPool.GetAvailableThreads: 2,043
ThreadPool.GetMaxThreads: 2,047
ThreadPool.GetMinThreads: 4
ThreadPool.CurrentThreads: 4
---- After Scheduling 500 ---- 17:01:55.2813
ThreadPool.GetAvailableThreads: 2,043
ThreadPool.GetMaxThreads: 2,047
ThreadPool.GetMinThreads: 4
ThreadPool.CurrentThreads: 4
---- After Sleeping 30.00s ---- 17:02:25.3403
ThreadPool.GetAvailableThreads: 2,022
ThreadPool.GetMaxThreads: 2,047
ThreadPool.GetMinThreads: 4
ThreadPool.CurrentThreads: 25
---- After Sleeping 30.00s ---- 17:02:55.3453
ThreadPool.GetAvailableThreads: 2,004
ThreadPool.GetMaxThreads: 2,047
ThreadPool.GetMinThreads: 4
ThreadPool.CurrentThreads: 43
---- After Waiting for all 500 ---- 17:03:25.3573
ThreadPool.GetAvailableThreads: 2,047
ThreadPool.GetMaxThreads: 2,047
ThreadPool.GetMinThreads: 4
ThreadPool.CurrentThreads: 0
Код
class Program
{
private static readonly List<Task> _tasks = new List<Task>();
static void Main(string[] args)
{
OutputThreadCount("Start");
var millisecondsDelay = 5000;
ScheduleTasks(1, millisecondsDelay);
OutputThreadCount("After Scheduling 1");
ScheduleTasks(50, millisecondsDelay);
OutputThreadCount("After Scheduling 50");
ScheduleTasks(500, millisecondsDelay);
OutputThreadCount("After Scheduling 500");
var all = Task.WhenAll(_tasks);
while (!all.IsCompleted)
{
var millisecondsTimeout = TimeSpan.FromSeconds(30);
Thread.Sleep(millisecondsTimeout);
OutputThreadCount($"After Sleeping {millisecondsTimeout.TotalSeconds:N2}");
}
OutputThreadCount("After Waiting for all 500");
Console.Read();
}
private static void ScheduleTasks(int taskCount, int millisecondsDelay)
{
for (int i = 0; i < taskCount; i++)
{
var task = Task.Run(() => Thread.Sleep(millisecondsDelay));
_tasks.Add(task);
}
}
private static void OutputThreadCount(string message)
{
Console.WriteLine();
Console.WriteLine($"---- {message} ---- {DateTime.UtcNow:HH:mm:ss.ffff}");
ThreadPool.GetAvailableThreads(out var availableWorkerThreads, out var availableCompletionPortThreads);
ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);
ThreadPool.GetMinThreads(out var minWorkerThreads, out var minCompletionPortThreads);
Console.WriteLine($"ThreadPool.GetAvailableThreads: {availableWorkerThreads:N0}");
Console.WriteLine($"ThreadPool.GetMaxThreads: {maxWorkerThreads:N0}");
Console.WriteLine($"ThreadPool.GetMinThreads: {minWorkerThreads:N0}");
Console.WriteLine($"ThreadPool.CurrentThreads: {maxWorkerThreads - availableWorkerThreads:N0}");
Console.WriteLine();
}
}