diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentBagExamples/StorageConcurrentBag.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentBagExamples/StorageConcurrentBag.cs index f4bd808ef4..f3c4a6e8a1 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentBagExamples/StorageConcurrentBag.cs +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentBagExamples/StorageConcurrentBag.cs @@ -28,6 +28,6 @@ public bool TryGet(out string? fileName) public int CountFiles() { - return _files.Count(); + return _files.Count; } } \ No newline at end of file diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentQueueExamples/UserLineConcurrentQueue.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentQueueExamples/UserLineConcurrentQueue.cs index 797c35b905..76c009159f 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentQueueExamples/UserLineConcurrentQueue.cs +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentQueueExamples/UserLineConcurrentQueue.cs @@ -41,6 +41,6 @@ public bool TryServeUser(out User? user) public int CountUsersInLine() { - return _users.Count(); + return _users.Count; } } \ No newline at end of file diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentStackExamples/PaperConcurrentStack.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentStackExamples/PaperConcurrentStack.cs index 636efeba56..7f8755f4ae 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentStackExamples/PaperConcurrentStack.cs +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ConcurrentStackExamples/PaperConcurrentStack.cs @@ -28,6 +28,6 @@ public bool TryGetTop(out string? color) public int Count() { - return _paperColor.Count(); + return _paperColor.Count; } } \ No newline at end of file diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerConcurrentDictionary.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerConcurrentDictionary.cs index cf6b3ed314..e10706f666 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerConcurrentDictionary.cs +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerConcurrentDictionary.cs @@ -1,5 +1,4 @@ -using System; -using ExampleApp.Helpers; +using ExampleApp.Helpers; using System.Collections.Concurrent; namespace ExampleApp.DictionaryExamples; @@ -27,7 +26,7 @@ public IDictionary GetAll() public void PrintAll() { - Console.WriteLine($"No. of Records: {_record.Count()}"); + Console.WriteLine($"No. of Records: {_record.Count}"); foreach (var (role, user) in _record) { Console.WriteLine($"Role: {role}, User: {user}"); diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerDictionary.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerDictionary.cs index 202bfafe25..6349b393ac 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerDictionary.cs +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/DictionaryExamples/RoleManagerDictionary.cs @@ -25,7 +25,7 @@ public IDictionary GetAll() public void PrintAll() { - Console.WriteLine($"No. of Records: {_record.Count()}"); + Console.WriteLine($"No. of Records: {_record.Count}"); foreach (var (role, user) in _record) { Console.WriteLine($"Role: {role}, User: {user}"); diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ExampleApp.csproj b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ExampleApp.csproj index 74abf5c976..5bf6c9b8cf 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ExampleApp.csproj +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ExampleApp.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net11.0 enable enable diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/Helpers/UserProvider.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/Helpers/UserProvider.cs index 0cb012368c..b4ce4d2760 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/Helpers/UserProvider.cs +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/Helpers/UserProvider.cs @@ -16,7 +16,7 @@ public class UserProvider public static async Task GetUser(int id) { - await Task.Delay((int)TimeSpan.FromSeconds(1).TotalMilliseconds); + await Task.Delay(TimeSpan.FromSeconds(1)); return _users[id]; } diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/ListAndSetExamples/ListAndSetAlternatives.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ListAndSetExamples/ListAndSetAlternatives.cs new file mode 100644 index 0000000000..a921a29504 --- /dev/null +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/ListAndSetExamples/ListAndSetAlternatives.cs @@ -0,0 +1,55 @@ +using System.Collections.Concurrent; + +namespace ExampleApp.ListAndSetExamples; + +public static class ListAndSetAlternatives +{ + public static int CountUniqueIds(int workers, int idsPerWorker) + { + var seenIds = new ConcurrentDictionary(); + + Parallel.For(0, workers, _ => + { + for (var id = 0; id < idsPerWorker; id++) + { + seenIds.TryAdd(id, 0); + } + }); + + return seenIds.Count; + } + + public static int AppendToQueue(int workers, int itemsPerWorker) + { + var entries = new ConcurrentQueue(); + + Parallel.For(0, workers, worker => + { + for (var i = 0; i < itemsPerWorker; i++) + { + entries.Enqueue(worker * itemsPerWorker + i); + } + }); + + return entries.Count; + } + + public static int AppendToLockedList(int workers, int itemsPerWorker) + { + var numbers = new List(); + var numbersLock = new Lock(); + + Parallel.For(0, workers, worker => + { + for (var i = 0; i < itemsPerWorker; i++) + { + lock (numbersLock) + { + numbers.Add(worker * itemsPerWorker + i); + } + } + }); + + return numbers.Count; + } +} diff --git a/collections-csharp/ConcurrentCollectionExample/ExampleApp/Program.cs b/collections-csharp/ConcurrentCollectionExample/ExampleApp/Program.cs index a2bf1fa867..b91a473e9b 100644 --- a/collections-csharp/ConcurrentCollectionExample/ExampleApp/Program.cs +++ b/collections-csharp/ConcurrentCollectionExample/ExampleApp/Program.cs @@ -1,12 +1,24 @@ using ExampleApp.DictionaryExamples; +using ExampleApp.ListAndSetExamples; -// Console.WriteLine("RunRoleManagerDictionaryExampleConcurrent()"); -// await RunRoleManagerDictionaryExampleConcurrent(); +if (args.Contains("--race")) +{ + Console.WriteLine("RunRoleManagerDictionaryExampleConcurrent()"); + await RunRoleManagerDictionaryExampleConcurrent(); + + return; +} + +Console.WriteLine("RunRoleManagerDictionaryExampleSync()"); +await RunRoleManagerDictionaryExampleSync(); -// Console.WriteLine("\n\n"); Console.WriteLine("RunRoleManagerConcurrentDictionaryExampleConcurrent()"); await RunRoleManagerConcurrentDictionaryExampleConcurrent(); +Console.WriteLine($"Unique ids: {ListAndSetAlternatives.CountUniqueIds(workers: 4, idsPerWorker: 1000)}"); +Console.WriteLine($"Queued entries: {ListAndSetAlternatives.AppendToQueue(workers: 4, itemsPerWorker: 1000)}"); +Console.WriteLine($"Locked list entries: {ListAndSetAlternatives.AppendToLockedList(workers: 4, itemsPerWorker: 1000)}"); + static async Task RunRoleManagerDictionaryExampleSync() { var roleManager = new RoleManagerDictionary(); diff --git a/collections-csharp/ConcurrentCollectionExample/Tests/ListAndSetAlternativesTests.cs b/collections-csharp/ConcurrentCollectionExample/Tests/ListAndSetAlternativesTests.cs new file mode 100644 index 0000000000..6a28909798 --- /dev/null +++ b/collections-csharp/ConcurrentCollectionExample/Tests/ListAndSetAlternativesTests.cs @@ -0,0 +1,30 @@ +using ExampleApp.ListAndSetExamples; + +namespace Tests; + +public class ListAndSetAlternativesTests +{ + [Fact] + public void GivenOverlappingIds_WhenAddedFromSeveralThreads_ThenEachIdIsStoredOnce() + { + var count = ListAndSetAlternatives.CountUniqueIds(workers: 4, idsPerWorker: 1000); + + Assert.Equal(1000, count); + } + + [Fact] + public void GivenSeveralWriters_WhenAppendingToConcurrentQueue_ThenNoItemIsLost() + { + var count = ListAndSetAlternatives.AppendToQueue(workers: 4, itemsPerWorker: 1000); + + Assert.Equal(4000, count); + } + + [Fact] + public void GivenSeveralWriters_WhenAppendingToLockedList_ThenNoItemIsLost() + { + var count = ListAndSetAlternatives.AppendToLockedList(workers: 4, itemsPerWorker: 1000); + + Assert.Equal(4000, count); + } +} diff --git a/collections-csharp/ConcurrentCollectionExample/Tests/PrinterColorPaperConcurrentStackTests.cs b/collections-csharp/ConcurrentCollectionExample/Tests/PrinterColorPaperConcurrentStackTests.cs index 3f82c93d09..223c79f48a 100644 --- a/collections-csharp/ConcurrentCollectionExample/Tests/PrinterColorPaperConcurrentStackTests.cs +++ b/collections-csharp/ConcurrentCollectionExample/Tests/PrinterColorPaperConcurrentStackTests.cs @@ -19,7 +19,7 @@ public async Task GivenColors_WhenAddIsInvokedConcurrently_ThenColorsAreAdded() } [Fact] - public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue() + public void GivenColor_WhenTryUseIsInvoked_ThenColorIsRemovedAndReturnsTrue() { var color = "white"; var papers = new PrinterColorPaperConcurrentStack(); @@ -31,7 +31,7 @@ public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue } [Fact] - public void GivenFileName_WhenTryGetIsInvoked_ThenFileIsRetrivedAndReturnsTrue() + public void GivenColor_WhenTryGetTopIsInvoked_ThenColorIsRetrievedAndReturnsTrue() { var color = "white"; var papers = new PrinterColorPaperConcurrentStack(); diff --git a/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerConcurrentDictionaryTests.cs b/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerConcurrentDictionaryTests.cs index 2336affb0b..a847cf917e 100644 --- a/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerConcurrentDictionaryTests.cs +++ b/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerConcurrentDictionaryTests.cs @@ -37,8 +37,8 @@ public async Task GivenDifferentRoles_WhenTryAssignIsInvokedConcurrently_ThenAll await Task.WhenAll(task1, task2, task3); var roles = concurrentRoleManager.GetAll(); var firstRoleExits = roles.TryGetValue(role, out User? user); - var secondRoleExits = roles.TryGetValue(role, out User? user1); - var thirdRoleExits = roles.TryGetValue(role, out User? user2); + var secondRoleExits = roles.TryGetValue(role1, out User? user1); + var thirdRoleExits = roles.TryGetValue(role2, out User? user2); Assert.True(firstRoleExits); Assert.True(secondRoleExits); diff --git a/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerDictionaryTests.cs b/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerDictionaryTests.cs index 5631729a49..9714ebbdd8 100644 --- a/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerDictionaryTests.cs +++ b/collections-csharp/ConcurrentCollectionExample/Tests/RoleManagerDictionaryTests.cs @@ -10,12 +10,12 @@ public class RoleManagerDictionaryTests public async Task GivenIdenticalRoles_WhenTryAssignIsInvoked_ThenOnlyOneUserIsAssigned() { var role = "Admin"; - var concurrentRoleManager = new RoleManagerConcurrentDictionary(); - await concurrentRoleManager.TryAssign(role, 1); - await concurrentRoleManager.TryAssign(role, 2); - await concurrentRoleManager.TryAssign(role, 3); + var roleManager = new RoleManagerDictionary(); + await roleManager.TryAssign(role, 1); + await roleManager.TryAssign(role, 2); + await roleManager.TryAssign(role, 3); - var roles = concurrentRoleManager.GetAll(); + var roles = roleManager.GetAll(); var valueExists = roles.TryGetValue(role, out User? user); Assert.True(valueExists); @@ -28,15 +28,15 @@ public async Task GivenDifferentRoles_WhenTryAssignIsInvoked_ThenAllUserAreAssig var role = "Admin"; var role1 = "Dev"; var role2 = "User"; - var concurrentRoleManager = new RoleManagerConcurrentDictionary(); - await concurrentRoleManager.TryAssign(role, 1); - await concurrentRoleManager.TryAssign(role1, 2); - await concurrentRoleManager.TryAssign(role2, 3); + var roleManager = new RoleManagerDictionary(); + await roleManager.TryAssign(role, 1); + await roleManager.TryAssign(role1, 2); + await roleManager.TryAssign(role2, 3); - var roles = concurrentRoleManager.GetAll(); + var roles = roleManager.GetAll(); var firstRoleExits = roles.TryGetValue(role, out User? user); - var secondRoleExits = roles.TryGetValue(role, out User? user1); - var thirdRoleExits = roles.TryGetValue(role, out User? user2); + var secondRoleExits = roles.TryGetValue(role1, out User? user1); + var thirdRoleExits = roles.TryGetValue(role2, out User? user2); Assert.True(firstRoleExits); Assert.True(secondRoleExits); diff --git a/collections-csharp/ConcurrentCollectionExample/Tests/StorageConcurrentBagTests.cs b/collections-csharp/ConcurrentCollectionExample/Tests/StorageConcurrentBagTests.cs index 73bc2e11d2..f144aa9cea 100644 --- a/collections-csharp/ConcurrentCollectionExample/Tests/StorageConcurrentBagTests.cs +++ b/collections-csharp/ConcurrentCollectionExample/Tests/StorageConcurrentBagTests.cs @@ -9,7 +9,7 @@ public async Task GivenFileNames_WhenAddFileIsInvokedConcurrently_ThenFilesAreAd { var storage = new StorageConcurrentBag(); var task1 = Task.Run(() => storage.AddNew("sample.txt")); - var task2 = Task.Run(() => storage.AddNew("awsome.png")); + var task2 = Task.Run(() => storage.AddNew("awesome.png")); var task3 = Task.Run(() => storage.AddNew("sample.pdf")); await Task.WhenAll(task1, task2, task3); @@ -20,7 +20,7 @@ public async Task GivenFileNames_WhenAddFileIsInvokedConcurrently_ThenFilesAreAd [Fact] public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue() { - var fileName = "awsome.png"; + var fileName = "awesome.png"; var storage = new StorageConcurrentBag(); storage.AddNew(fileName); var result = storage.TryRemove(out string? removedFile); @@ -30,9 +30,9 @@ public void GivenFileName_WhenTryRemoveIsInvoked_ThenFileIsRemovedAndReturnsTrue } [Fact] - public void GivenFileName_WhenTryGetIsInvoked_ThenFileIsRetrivedAndReturnsTrue() + public void GivenFileName_WhenTryGetIsInvoked_ThenFileIsRetrievedAndReturnsTrue() { - var fileName = "awsome.png"; + var fileName = "awesome.png"; var storage = new StorageConcurrentBag(); storage.AddNew(fileName); var result = storage.TryGet(out string? retrievedFile); diff --git a/collections-csharp/ConcurrentCollectionExample/Tests/Tests.csproj b/collections-csharp/ConcurrentCollectionExample/Tests/Tests.csproj index 17c423821e..562bba64d4 100644 --- a/collections-csharp/ConcurrentCollectionExample/Tests/Tests.csproj +++ b/collections-csharp/ConcurrentCollectionExample/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net11.0 enable enable @@ -9,13 +9,13 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all