forked from 2881099/WorkQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkQueue.cs
198 lines (176 loc) · 3.81 KB
/
WorkQueue.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
public class WorkQueue : WorkQueue<Action> {
public WorkQueue() : this(16, -1) { }
public WorkQueue(int thread)
: this(thread, -1) {
}
public WorkQueue(int thread, int capacity) {
base.Thread = thread;
base.Capacity = capacity;
base.Process += delegate(Action ah) {
ah();
};
}
}
public class WorkQueue<T> : IDisposable {
public delegate void WorkQueueProcessHandler(T item);
public event WorkQueueProcessHandler Process;
private int _thread = 16;
private int _capacity = -1;
private int _work_index = 0;
private Dictionary<int, WorkInfo> _works = new Dictionary<int, WorkInfo>();
private object _works_lock = new object();
private Queue<T> _queue = new Queue<T>();
private object _queue_lock = new object();
public WorkQueue() : this(16, -1) { }
public WorkQueue(int thread)
: this(thread, -1) {
}
public WorkQueue(int thread, int capacity) {
_thread = thread;
_capacity = capacity;
}
public void Enqueue(T item) {
lock (_queue_lock) {
if (_capacity > 0 && _queue.Count >= _capacity) throw new StackOverflowException($"队列数量大于(capacity)设置值:{_capacity}");
_queue.Enqueue(item);
}
lock (_works_lock) {
foreach (WorkInfo w in _works.Values) {
if (w.IsWaiting) {
w.Set();
return;
}
}
}
if (_works.Count < _thread) {
if (_queue.Count > 0) {
int index = 0;
lock (_works_lock) {
index = _work_index++;
_works.Add(index, new WorkInfo());
}
new Thread(delegate() {
WorkInfo work = _works[index];
while (true) {
List<T> de = new List<T>();
if (_queue.Count > 0) {
lock (_queue_lock) {
if (_queue.Count > 0) {
de.Add(_queue.Dequeue());
}
}
}
if (de.Count > 0) {
try {
this.OnProcess(de[0]);
} catch {
}
}
if (_queue.Count == 0) {
work.WaitOne(TimeSpan.FromSeconds(20));
if (_queue.Count == 0) {
break;
}
}
}
lock (_works_lock) {
_works.Remove(index);
}
work.Dispose();
}).Start();
}
}
}
protected virtual void OnProcess(T item) {
if (Process != null) {
Process(item);
}
}
#region IDisposable 成员
public void Dispose() {
lock (_queue_lock) {
_queue.Clear();
}
lock (_works_lock) {
foreach (WorkInfo w in _works.Values) {
w.Dispose();
}
}
}
#endregion
public int Thread {
get { return _thread; }
set {
if (_thread != value) {
_thread = value;
}
}
}
public int Capacity {
get { return _capacity; }
set {
if (_capacity != value) {
_capacity = value;
}
}
}
public int UsedThread {
get { return _works.Count; }
}
public int Queue {
get { return _queue.Count; }
}
public string Statistics {
get {
string value = string.Format(@"线程:{0}/{1}
队列:{2}
", _works.Count, _thread, _queue.Count);
int[] keys = new int[_works.Count];
try {
_works.Keys.CopyTo(keys, 0);
} catch {
lock (_works_lock) {
keys = new int[_works.Count];
_works.Keys.CopyTo(keys, 0);
}
}
foreach (int k in keys) {
WorkInfo w = null;
if (_works.TryGetValue(k, out w)) {
value += string.Format(@"线程{0}:{1}
", k, w.IsWaiting);
}
}
return value;
}
}
class WorkInfo : IDisposable {
private ManualResetEvent _reset = new ManualResetEvent(false);
private bool _isWaiting = false;
public void WaitOne(TimeSpan timeout) {
try {
_reset.Reset();
_isWaiting = true;
_reset.WaitOne(timeout);
} catch { }
}
public void Set() {
try {
_isWaiting = false;
_reset.Set();
} catch { }
}
public bool IsWaiting {
get { return _isWaiting; }
}
#region IDisposable 成员
public void Dispose() {
this.Set();
}
#endregion
}
}