Repository navigation
Expand file tree
/
Copy pathThreadLock.cs
More file actions
58 lines (51 loc) · 2.09 KB
/
Copy pathThreadLock.cs
File metadata and controls
58 lines (51 loc) · 2.09 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
namespace Algorithm.MultiThread
{
class ThreadLock
{
static System.Collections.Generic.List<int> arr = new System.Collections.Generic.List<int>();
public static void test()
{
for (int i = 0; i < 100; i++)
{
arr.Add(i);
}
System.Threading.Thread t1 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(removeItem));
t1.Name = "GDI";
t1.Start(null);
System.Threading.Thread t2 = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(removeItem));
t2.Name = "NOD";
t2.Start(null);
}
static System.Threading.ReaderWriterLock rwl = new System.Threading.ReaderWriterLock();
static object CodeLock = new object(); //LOCK the code with lock keyword or Moniter
//[Synchronization(SynchronizationOption.Required)] //SynchronizationAttribute from System.EnterpriseServices.dll (Synchronise the method)
private static void removeItem(object o)
{
while (arr.Count > 0)
{
//lock (CodeLock)
//{
//System.Threading.Monitor.Enter(CodeLock);
//System.Threading.Monitor.Wait(CodeLock);
//System.Threading.Monitor.Pulse(CodeLock);
//rwl.AcquireReaderLock(1000);
System.Console.WriteLine(System.Threading.Thread.CurrentThread.Name + " get " + arr[0]);
//rwl.ReleaseReaderLock();
//rwl.AcquireWriterLock(1000);
arr.RemoveAt(0);
//rwl.ReleaseWriterLock();
System.Threading.Thread.Sleep(1000);
//System.Threading.Monitor.Exit(CodeLock);
//}
}
}
static void test2()
{
//System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(printX));
}
private static void printX()
{
}
}
}