-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDirtyPool.cs
More file actions
168 lines (132 loc) · 4.81 KB
/
DirtyPool.cs
File metadata and controls
168 lines (132 loc) · 4.81 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ThreadPoolLockExample
{
public class DirtyPool
{
// Data held in this queue.
public static Queue<string> _Operations
= new Queue<string>();
// Status objects for printer delegate/thred
// to know when to exit.
bool OneEnded;
bool TwoEnded;
public static void ShowExample()
{
DirtyPool dp = new DirtyPool();
dp.DoIt();
// Sleep long enough so not to kill the threads.
Thread.Sleep(10000);
}
public void SetData(string who)
{
Console.WriteLine(who + " is finished process");
lock (_Operations)
_Operations.Enqueue(who);
}
public void PrintData()
{
lock (_Operations)
if (_Operations.Count > 0)
Console.WriteLine("out="+_Operations.Dequeue());
}
public void DoIt()
{
ThreadPool.QueueUserWorkItem(
delegate // Anonomous Delegate For Creation 1
{
for (int index = 0; index < 5; ++index)
{
Thread.Sleep(1500);
SetData("Pool 1: " + index);
}
OneEnded = true;
}
);
ThreadPool.QueueUserWorkItem(
delegate // Anonomous Delegate For Creation 2
{
for (int index = 0; index < 5; ++index)
{
Thread.Sleep(1000);
SetData("Pool 2: " + index);
}
TwoEnded = true;
}
);
ThreadPool.QueueUserWorkItem(
delegate // Anonomous Delegate For Extraction & Printing
{
while (!(TwoEnded && OneEnded))
PrintData();
PrintData(); // Just in case...
}
);
}
//---------------
private object sync = new object();
public List<string> IDList { get { return idList; } set { idList = value; } }
private List<string> idList = new List<string>();
public List<string> Calculate(int n)
{
int divide = idList.Count / 10;
List<string> perList = new List<string>();
for (int i = divide * n; i < divide * (n + 1); i++)
{
perList.Add(idList[i]);
}
return perList;
}
public static void AbelShowExample()
{
List<string> list = new List<string>();
for (int i = 0; i < 100; i++)
{
list.Add("test" + i);
}
DirtyPool dp = new DirtyPool();
dp.IDList = list;
dp.AbelTest();
// Sleep long enough so not to kill the threads.
Thread.Sleep(10000);
}
public void WriteDB(List<string> list,Object threadContext)
{
Console.WriteLine(threadContext.ToString() + " is writing db");
}
public void ThreadPoolCallback(Object threadContext)
{
System.Threading.Thread.Sleep(10000);
int threadIndex = (int)threadContext;
Console.WriteLine("thread {0} started...", threadIndex);
List<string> mylist = Calculate(threadIndex);
for (int i = 0; i < mylist.Count ; i++)
{
Console.WriteLine("thread {0} started...{1}", threadIndex,mylist[i]);
}
lock (sync)
{
WriteDB(mylist,threadContext);
}
doneEvents[threadIndex].Set();
}
const int IDHandlerThreadCount = 5;
ManualResetEvent[] doneEvents = new ManualResetEvent[IDHandlerThreadCount];
public void AbelTest()
{
List<string> totalzipcodeList = new List<string>();
for (int i = 0; i < IDHandlerThreadCount; i++)
{
doneEvents[i] = new ManualResetEvent(false);
// ThreadHelper f = new ThreadHelper(i, doneEvents[i]);
// fibArray[i] = f;
ThreadPool.QueueUserWorkItem(ThreadPoolCallback, i);
}
WaitHandle.WaitAll(doneEvents);
Console.WriteLine("All calculations are complete.");
}
}
}