Repository navigation
Expand file tree
/
Copy pathDoubleQueue.java
More file actions
296 lines (266 loc) · 6.02 KB
/
Copy pathDoubleQueue.java
File metadata and controls
296 lines (266 loc) · 6.02 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.util.AbstractQueue;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
/**
* 创建人:sp.fu <br>
* 功能描述:双缓冲工作队列,线程安全 <br>
* 版本: <br>
* ====================================== <br>
* 修改记录 <br>
* ====================================== <br>
* 序号 姓名 日期 版本 简单描述 <br>
*
*/
public class DoubleQueue<E> extends AbstractQueue<E> implements
BlockingQueue<E>, java.io.Serializable {
private static final long serialVersionUID = 1011398447523020L;
public static final int DEFALUT_QUEUE_CAPACITY = 50000;
private Logger logger = Logger.getLogger(DoubleQueue.class.getName());
/** The read lock */
private ReentrantLock readLock;
//写锁
private ReentrantLock writeLock;
//是否满,信号量
private Condition notFull;
private Condition awake;
//读写数组
private transient E[] writeArray;
private transient E[] readArray;
//读写计数
private volatile int writeCount;
private volatile int readCount;
//写数组下标指针
private int writeArrayTP;
private int writeArrayHP;
//读数组下标指针
private int readArrayTP;
private int readArrayHP;
private int capacity;
public DoubleQueue(int capacity) {
//默认
this.capacity = DEFALUT_QUEUE_CAPACITY;
if (capacity > 0) {
this.capacity = capacity;
}
readArray = (E[])new Object[capacity];
writeArray = (E[])new Object[capacity];
readLock = new ReentrantLock();
writeLock = new ReentrantLock();
notFull = writeLock.newCondition();
awake = writeLock.newCondition();
}
private void insert(E e) {
writeArray[writeArrayTP] = e;
++writeArrayTP;
++writeCount;
}
private E extract() {
E e = readArray[readArrayHP];
readArray[readArrayHP] = null;
++readArrayHP;
--readCount;
return e;
}
/**
* 交换读写队列指针
* @param timeout
* @param isInfinite
* @throws InterruptedException
*/
private long queueSwap(long timeout, boolean isInfinite)
throws InterruptedException {
writeLock.lock();
try {
if (writeCount <= 0) {
logger.debug("Write Count:" + writeCount
+ ", Write Queue is empty, do not switch!");
try {
logger.debug("Queue is empty, need wait....");
if (isInfinite && timeout <= 0) {
awake.await();
return -1;
} else {
return awake.awaitNanos(timeout);
}
} catch (InterruptedException ie) {
awake.signal();
throw ie;
}
} else {
E[] tmpArray = readArray;
readArray = writeArray;
writeArray = tmpArray;
readCount = writeCount;
readArrayHP = 0;
readArrayTP = writeArrayTP;
writeCount = 0;
writeArrayHP = readArrayHP;
writeArrayTP = 0;
notFull.signal();
logger.debug("Queue switch successfully!");
return -1;
}
} finally {
writeLock.unlock();
}
}
@Override
public boolean offer(E e, long timeout, TimeUnit unit)
throws InterruptedException {
if (e == null) {
throw new NullPointerException();
}
long nanoTime = unit.toNanos(timeout);
writeLock.lockInterruptibly();
try {
for (;;) {
if (writeCount < writeArray.length) {
insert(e);
if (writeCount == 1) {
awake.signal();
}
return true;
}
// Time out
if (nanoTime <= 0) {
logger.debug("offer wait time out!");
return false;
}
// keep waiting
try {
logger.debug("Queue is full, need wait....");
nanoTime = notFull.awaitNanos(nanoTime);
} catch (InterruptedException ie) {
notFull.signal();
throw ie;
}
}
} finally {
writeLock.unlock();
}
}
//取
@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
long nanoTime = unit.toNanos(timeout);
readLock.lockInterruptibly();
try {
for (;;) {
if (readCount > 0) {
return extract();
}
if (nanoTime <= 0) {
logger.debug("poll time out!");
return null;
}
nanoTime = queueSwap(nanoTime, false);
}
} finally {
readLock.unlock();
}
}
//等待500毫秒
@Override
public E poll() {
E ret = null;
try {
ret = poll(500L, TimeUnit.MILLISECONDS);
} catch (Exception e) {
ret = null;
}
return ret;
}
//查看
@Override
public E peek() {
E e = null;
readLock.lock();
try {
if (readCount > 0) {
e = readArray[readArrayHP];
}
} finally {
readLock.unlock();
}
return e;
}
//默认500毫秒
@Override
public boolean offer(E e) {
boolean ret = false;
try {
ret = offer(e, 500L, TimeUnit.MILLISECONDS);
} catch (Exception e2) {
ret = false;
}
return ret;
}
@Override
public void put(E e) throws InterruptedException {
offer(e, 500L, TimeUnit.MILLISECONDS); // never need to block
}
@Override
public E take() throws InterruptedException {
return poll(500L, TimeUnit.MILLISECONDS);
}
@Override
public int remainingCapacity() {
return this.capacity;
}
@Override
public int drainTo(Collection<? super E> c) {
return 0;
}
@Override
public int drainTo(Collection<? super E> c, int maxElements) {
return 0;
}
@Override
public Iterator<E> iterator() {
return null;
}
//当前读队列中还有多少个
@Override
public int size() {
int size = 0;
readLock.lock();
try {
if (readCount > 0) {
size = readCount;
}
} finally {
readLock.unlock();
}
return size;
}
//清理
public void clear() {
writeLock.lock();
try {
readCount = 0;
readArrayHP = 0;
writeCount = 0;
writeArrayTP = 0;
logger.debug("Queue clear successfully!");
} finally {
writeLock.unlock();
}
}
public static void main(String[] args) {
try {
DoubleQueue<String> q = new DoubleQueue<String>(1000);
q.put("shiping");
q.put("fu");
q.poll();
System.out.println(q.size());
} catch (InterruptedException e) {
// Auto-generated catch block
e.printStackTrace();
}
}
}