ThreadPool-MaxThreads-AvailableThreads =当前线程池线程数吗?

时间:2019-06-04 17:12:55

标签: c# .net multithreading threadpool

以下内容将为我提供准确数量的ThreadPool线程吗?

ThreadPool.GetAvailableThreads(out var availableWorkerThreads, out var availableCompletionPortThreads);
ThreadPool.GetMaxThreads(out var maxWorkerThreads, out var maxCompletionPortThreads);

var currentWorkerThreads = maxWorkerThreads - availableWorkerThreads;
var currentCompletionPortThreads = maxCompletionPortThreads - availableCompletionPortThreads;

Console.WriteLine("Current Worker Threads: " + currentWorkerThreads)
Console.WriteLine("Current Completion Port Threads: " + currentCompletionPortThreads)

1 个答案:

答案 0 :(得分:0)

看来您确实可以使用它来计算活动线程池线程的数量。

至少在.NET Framework 4.6.1上

测试

我编写了一个noddy控制台应用程序,通过调度一堆任务并查看创建了多少线程来进行测试。

您可以看到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();
    }
}