Created
June 11, 2022 00:13
-
-
Save mdcarmo/2e9cc160ad4310c099ccc8665e93b4db to your computer and use it in GitHub Desktop.
This file contains 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
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