Skip to content

Instantly share code, notes, and snippets.

@yapaxi
Created May 30, 2021 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yapaxi/cfc529bee9bd5361482051d45a62a571 to your computer and use it in GitHub Desktop.
Save yapaxi/cfc529bee9bd5361482051d45a62a571 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp37
{
class Program
{
static async Task Main(string[] args)
{
// simple starvation
ThreadPool.SetMinThreads(30, 30);
while (true)
{
var httpClient = new HttpClient();
ThreadPool.GetMinThreads(out var wt, out var cpt);
Console.WriteLine($"thread pool threads: cur={ThreadPool.ThreadCount} | min-worker={wt} | min-completion={cpt}");
var getGoogle = Task.Run(async () =>
{
var sw = Stopwatch.StartNew();
await Task.Delay(1).ConfigureAwait(false);
httpClient.GetAsync("https://google.com").GetAwaiter().GetResult();
return sw.Elapsed;
});
var lazyWaitsForGoogle = new Lazy<object>(() =>
{
getGoogle.GetAwaiter().GetResult();
return new object();
});
var incomingLoad = new List<Task>();
do
{
incomingLoad.Add(Task.Run(async () =>
{
await Task.Delay(1).ConfigureAwait(false);
return lazyWaitsForGoogle.Value;
}));
if (incomingLoad.Count % Environment.ProcessorCount == 0)
{
await Task.Delay(1).ConfigureAwait(false);
}
}
while (!getGoogle.IsCompleted);
Console.WriteLine($"[load] added: {incomingLoad.Count}");
await Task.WhenAll(incomingLoad).ConfigureAwait(false);
Console.WriteLine("[load] done");
var elapsed = await getGoogle.ConfigureAwait(false);
Console.WriteLine($"[target] done: {elapsed}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment