-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
121 lines (103 loc) · 3.22 KB
/
Program.cs
File metadata and controls
121 lines (103 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Collections.Concurrent;
namespace ConcurrentCollections;
// System.Collections.Concurrent Namespace
// https://learn.microsoft.com/en-us/dotnet/api/system.collections.concurrent?view=net-9.0
static class Program
{
public static void Main()
{
//ConcurrentDictionaryExample();
//ConcurrentBagExample();
//ThreadSafeListLikeAsConcurrentList();
//ConcurrentQueueExample();
//ConcurrentStackExample();
//BlockingCollectionExample();
}
static readonly ConcurrentDictionary<int, string> Dictionary = new ConcurrentDictionary<int, string>();
static void ConcurrentDictionaryExample()
{
Parallel.For(0, 100, i =>
{
var added = Dictionary.TryAdd(i, $"Value-{i}");
if (added)
{
Console.WriteLine($"Thread {Thread.CurrentThread.ManagedThreadId} added key {i}");
}
});
Console.WriteLine("\nAll items in dictionary:");
foreach (var kvp in Dictionary)
{
Console.WriteLine($"{kvp.Key} => {kvp.Value}");
}
}
static readonly ConcurrentBag<int> Bag = new ConcurrentBag<int>();
static void ConcurrentBagExample()
{
Parallel.For(0, 100, i =>
{
Bag.Add(i);
});
Console.WriteLine($"Count: {Bag.Count}");
}
static readonly List<int> List = new List<int>();
static readonly object Locker = new object();
static void ThreadSafeListLikeAsConcurrentList()
{
Parallel.For(0, 100, i =>
{
lock (Locker)
{
List.Add(i);
}
});
Console.WriteLine($"Count: {List.Count}");
}
static readonly ConcurrentQueue<int> Queue = new ConcurrentQueue<int>();
static void ConcurrentQueueExample()
{
Parallel.For(0, 100, i =>
{
Queue.Enqueue(i);
});
Console.WriteLine($"Queue Count After Enqueue: {Queue.Count}");
while (Queue.TryDequeue(out var result))
{
Console.WriteLine($"Dequeued: {result}");
}
}
static readonly ConcurrentStack<int> Stack = new ConcurrentStack<int>();
static void ConcurrentStackExample()
{
Parallel.For(0, 100, i =>
{
Stack.Push(i);
});
Console.WriteLine($"Stack Count After Push: {Stack.Count}");
while (Stack.TryPop(out var result))
{
Console.WriteLine($"Popped: {result}");
}
}
static readonly BlockingCollection<int> Blocking = new BlockingCollection<int>(boundedCapacity: 10);
static void BlockingCollectionExample()
{
var producer = Task.Run(() =>
{
for (int i = 0; i < 20; i++)
{
Blocking.Add(i);
Console.WriteLine($"Produced: {i}");
}
Blocking.CompleteAdding();
});
var consumer = Task.Run(() =>
{
foreach (var item in Blocking.GetConsumingEnumerable())
{
Console.WriteLine($"Consumed: {item}");
Thread.Sleep(100); // Simulate work
}
});
Task.WaitAll(producer, consumer);
}
}