Created
August 7, 2020 02:12
-
-
Save waf/5c0cc2d7263ba97d6e56215a053a3971 to your computer and use it in GitHub Desktop.
Testing threadpool behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace ThreadPoolLogger | |
{ | |
class Program | |
{ | |
const int ThreadChange = 40; | |
static async Task Main(string[] args) | |
{ | |
LogAvailableThreads(); | |
Console.WriteLine(); | |
LogMinMaxConfiguration(); | |
Console.WriteLine(); | |
var success = ThreadPool.SetMinThreads( | |
workerThreads: ThreadChange, | |
completionPortThreads: ThreadChange | |
); | |
Console.WriteLine($"Setting minimum worker and IOCP threads to {ThreadChange}: {success}"); | |
Console.WriteLine(); | |
LogMinMaxConfiguration(); | |
Console.WriteLine(); | |
Console.WriteLine(DateTime.Now.ToLongTimeString() + ": Doing some work... please wait 15 seconds"); | |
await Task.WhenAll( | |
Enumerable | |
.Range(1, 100) | |
.Select(async i => | |
{ | |
await Task.Delay(TimeSpan.FromSeconds(5)); // async delay -- IOCP | |
Thread.Sleep(TimeSpan.FromSeconds(10)); // blocking delay | |
}) | |
); | |
Console.WriteLine(DateTime.Now.ToLongTimeString() + ": Work done."); | |
LogAvailableThreads(); | |
} | |
private static void LogMinMaxConfiguration() | |
{ | |
ThreadPool.GetMinThreads(out int minWorkerThreads, out int minIoCompletionPortThreads); | |
Console.WriteLine($"Minimum Worker Threads: {minWorkerThreads}, Minimum IOCP Threads: {minIoCompletionPortThreads}"); | |
ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxIoCompletionPortThreads); | |
Console.WriteLine($"Maximum Worker Threads: {maxWorkerThreads}, Maximum IOCP Threads: {maxIoCompletionPortThreads}"); | |
} | |
private static void LogAvailableThreads() | |
{ | |
ThreadPool.GetAvailableThreads(out int currentWorkerThreads, out int currentIoCompletionPortThreads); | |
Console.WriteLine($"Available Worker Threads: {currentWorkerThreads}, Available IOCP Threads: {currentIoCompletionPortThreads}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment