-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment.cs
117 lines (102 loc) · 3.36 KB
/
assignment.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
using System;
using System.Threading;
public static class MutexWaitHanldes
{
public static Mutex _handleOne = new Mutex(true);
public static Mutex _handleTwo = new Mutex(false);
}
public class DAO
{
public readonly static DAO Instance = new DAO();
public Mutex _handleOne = new Mutex(true);
public Mutex _handleTwo = new Mutex(true);
private DAO() { }
// [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public void Create()
{
Console.WriteLine("Create Action Awaiting For Signal");
this._handleOne.WaitOne();
try
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Create Action.......");
Thread.Sleep(1000);
if (i == 5) { return; }
}
}
finally
{
this._handleOne.ReleaseMutex();
}
}
//[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public void Delete()
{
Console.WriteLine("Delete Action Awaiting For Signal");
this._handleOne.WaitOne();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Delete Action.......");
Thread.Sleep(500);
}
this._handleOne.ReleaseMutex();
}
//[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public void Update()
{
Console.WriteLine("Update Action Awaiting For Signal");
this._handleOne.WaitOne();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Update Action.......");
Thread.Sleep(800);
}
this._handleOne.ReleaseMutex();
}
//[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public void Select()
{
Console.WriteLine("Select Action Awaiting For Signal");
this._handleTwo.WaitOne();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("Select Action.......");
Thread.Sleep(800);
}
this._handleTwo.ReleaseMutex();
}
// [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.Synchronized)]
public void SelectByKey()
{
Console.WriteLine("SelectKey Action Awaiting For Signal");
this._handleTwo.WaitOne();
for (int i = 0; i < 10; i++)
{
Console.WriteLine("SelectKey Action.......");
Thread.Sleep(800);
}
this._handleTwo.ReleaseMutex();
}
}
class SynchronizationDemo
{
static void Main()
{
DAO singleton = DAO.Instance;
new Thread(singleton.Create).Start();
new Thread(singleton.Delete).Start();
new Thread(singleton.Update).Start();
new Thread(singleton.Select).Start();
new Thread(singleton.SelectByKey).Start();
Console.WriteLine("Click Here To Start Thread Actions");
Console.ReadKey();
singleton._handleOne.ReleaseMutex();
singleton._handleTwo.ReleaseMutex();
singleton._handleOne.WaitOne();
singleton._handleTwo.WaitOne();
Console.WriteLine("End OF Main");
singleton._handleOne.ReleaseMutex();
singleton._handleTwo.ReleaseMutex();
}
}