Skip to content

Instantly share code, notes, and snippets.

@mdcarmo
Created June 11, 2022 00:13
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 mdcarmo/2e9cc160ad4310c099ccc8665e93b4db to your computer and use it in GitHub Desktop.
Save mdcarmo/2e9cc160ad4310c099ccc8665e93b4db to your computer and use it in GitHub Desktop.
class Program
{
static void Main(string[] args)
{
var timeExecution = new Stopwatch();
Console.WriteLine($"Iniciando a execução");
timeExecution.Start();
InitProcess();
timeExecution.Stop();
Console.WriteLine($"Execução finalizada: {timeExecution.Elapsed:m\\ss\\.fff}");
Console.ReadKey();
}
static void InitProcess()
{
var semaphore = new SemaphoreSlim(1);
var listTasks = new List<Task>();
for (int i = 0; i < 20; i++)
{
listTasks.Add(ProcessWithSemaphore(semaphore));
//listTasks.Add(ProcessSemaphoreless());
}
Task.WaitAll(listTasks.ToArray());
}
static async Task ProcessWithSemaphore(SemaphoreSlim semaphoreSlim)
{
await semaphoreSlim.WaitAsync();
Console.WriteLine("Simulando a chamada a uma API qualquer com semaphore");
var _httpClient = HttpClientFactory.Create();
await _httpClient.GetAsync("http://httpstat.us/200?sleep=1000");
semaphoreSlim.Release();
}
static async Task ProcessSemaphoreless()
{
Console.WriteLine("Simulando a chamada a uma API qualquer sem semaphore");
var _httpClient = HttpClientFactory.Create();
await _httpClient.GetAsync("http://httpstat.us/200?sleep=1000");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment