-
Notifications
You must be signed in to change notification settings - Fork 14
/
WorkQueue.cs
204 lines (184 loc) · 5 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
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
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 ConcurrentDictionary<int, WorkInfo> _works = new ConcurrentDictionary<int, WorkInfo>();
private ConcurrentQueue<T> _queue = new ConcurrentQueue<T>();
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)
{
if (_capacity > 0 && _queue.Count >= _capacity) throw new Exception($"队列数量大于(capacity)设置值:{_capacity}");
_queue.Enqueue(item);
foreach (WorkInfo w in _works.Values)
{
if (w.IsWaiting)
{
w.Set();
return;
}
}
if (_works.Count < _thread)
{
if (_queue.Count > 0)
{
int index = Interlocked.Increment(ref _work_index);
var work = new WorkInfo();
_works.TryAdd(index, work);
new Thread(delegate () {
while (isdisposed == false)
{
if (_queue.TryDequeue(out var de))
{
try
{
this.OnProcess(de);
}
catch
{
}
}
if (_queue.Count == 0)
{
if (work.WaitOne(TimeSpan.FromSeconds(20))) continue;
if (_queue.Count == 0) break;
}
}
_works.TryRemove(index, out var oldw);
work.Dispose();
}).Start();
}
}
}
protected virtual void OnProcess(T item)
{
if (Process != null)
Process(item);
}
#region IDisposable 成员
bool isdisposed = false;
object isdisposedLock = new object();
public void Dispose()
{
if (isdisposed) return;
lock (isdisposedLock)
{
if (isdisposed) return;
isdisposed = true;
}
while (_queue.TryDequeue(out var de)) ;
foreach (WorkInfo w in _works.Values)
w.Dispose();
}
#endregion
public int Thread
{
get => _thread;
set
{
if (_thread != value)
_thread = value;
}
}
public int Capacity
{
get => _capacity;
set
{
if (_capacity != value)
_capacity = value;
}
}
public int UsedThread => _works.Count;
public int Queue => _queue.Count;
public string Statistics
{
get
{
string value = string.Format(@"线程:{0}/{1}
队列:{2}
", _works.Count, _thread, _queue.Count);
foreach (var k in _works.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 bool WaitOne(TimeSpan timeout)
{
if (isdisposed) return false;
try
{
_reset.Reset();
_isWaiting = true;
return _reset.WaitOne(timeout);
}
catch { }
return false;
}
public bool Set()
{
try
{
_isWaiting = false;
return _reset.Set();
}
catch { }
return false;
}
public bool IsWaiting => _isWaiting;
bool isdisposed = false;
object isdisposedLock = new object();
public void Dispose()
{
if (isdisposed) return;
lock (isdisposedLock)
{
if (isdisposed) return;
isdisposed = true;
}
this.Set();
_reset.Dispose();
}
}
}