-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_Monitors.cs
124 lines (105 loc) · 3.79 KB
/
_Monitors.cs
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
122
123
124
using Common.Interfaces;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Ch05.Examples
{
public class _Monitors : IBaseExecutor
{
private IEnumerable<int> Range = Enumerable.Range(1, 100);
public void Run()
{
//LockExample();
//MutexExample();
SemaphoreExample();
}
#region Semaphore
Semaphore Semaphore = new Semaphore(10, 10); //Local Semaphore
// Any semaphore that is created with a name will be created as a global semaphore
//Semaphore Semaphore = new Semaphore(5, 5, "Globalsemaphore"); //Global Semaphore
private void SemaphoreExample()
{
Range.AsParallel().ForAll(i =>
{
Semaphore.WaitOne();
Console.WriteLine($"{Environment.NewLine}Index {i} making service call using Task {Task.CurrentId}");
//Simulate Http call
try
{
DummyService(i);
}
catch (Exception e)
{
Console.WriteLine($"{Environment.NewLine}-------------------- Exception Handled on {Task.CurrentId} | {e.Message} --------------------");
}
Console.WriteLine($"Index {i} releasing semaphore using Task {Task.CurrentId}");
Semaphore.Release();
});
}
private static void DummyService(int i)
{
var rand = new Random().Next(1000, 1010);
Console.WriteLine($"Index {i} waiting for: {rand} ms, using Task {Task.CurrentId}");
if (rand.Equals(1005))
{
Console.WriteLine($"######## Index {i} throw an Exception, using Task {Task.CurrentId} ########");
throw new Exception("1005 Exception");
}
Thread.Sleep(rand);
}
#endregion
#region Mutex
//if we try to run it on multiple instances with unnamedMutex, it will throw an IOException
//private static Mutex mutex = new Mutex();
private static Mutex mutex = new Mutex(false, "ShaktiSinghTanwar");
private void MutexExample()
{
Stopwatch watch = Stopwatch.StartNew();
Range.AsParallel().AsOrdered().ForAll(i =>
{
Thread.Sleep(10);
mutex.WaitOne();
File.AppendAllText("MutexExample.txt", $"{i} | ");
mutex.ReleaseMutex();
});
watch.Stop();
Console.WriteLine($"Total time to write file is { watch.ElapsedMilliseconds} ms");
}
#endregion
#region Lock
static object _locker = new object();
private void LockExample()
{
Stopwatch watch = Stopwatch.StartNew();
//NonparallelAppendText(range);
LockParallelAppendText(Range);
watch.Stop();
Console.WriteLine($"Total time to write file is { watch.ElapsedMilliseconds} ms");
}
private static void NonparallelAppendText(IEnumerable<int> range)
{
for (int i = 0; i < range.Count(); i++)
{
Thread.Sleep(10);
File.AppendAllText("LockExample.txt", $"{i} | ");
}
}
private static void LockParallelAppendText(IEnumerable<int> range)
{
range.AsParallel().AsOrdered().ForAll(i =>
{
Thread.Sleep(10);
lock (_locker)
{
Thread.Sleep(10);
File.AppendAllText("LockExample.txt", $"{i} | ");
}
});
}
#endregion
}
}