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 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*
File: BoundedLinkedQueue.java
Originally written by Doug Lea and released into the public domain.
This may be used for any purposes whatsoever without acknowledgment.
Thanks for the assistance and support of Sun Microsystems Labs,
and everyone contributing, testing, and using this code.
History:
Date Who What
11Jun1998 dl Create public version
17Jul1998 dl Simplified by eliminating wait counts
25aug1998 dl added peek
10oct1999 dl lock on node object to ensure visibility
27jan2000 dl setCapacity forces immediate permit reconcile
*/
package EDU.oswego.cs.dl.util.concurrent;
/**
* A bounded variant of
* LinkedQueue
* class. This class may be
* preferable to
* BoundedBuffer
* because it allows a bit more
* concurency among puts and takes, because it does not
* pre-allocate fixed storage for elements, and allows
* capacity to be dynamically reset.
* On the other hand, since it allocates a node object
* on each put, it can be slow on systems with slow
* allocation and GC.
* Also, it may be
* preferable to
* LinkedQueue
* when you need to limit
* the capacity to prevent resource exhaustion. This protection
* normally does not hurt much performance-wise: When the
* queue is not empty or full, most puts and
* takes are still usually able to execute concurrently.
* @see LinkedQueue
* @see BoundedBuffer
* <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>] <p>
**/
public class BoundedLinkedQueue implements BoundedChannel {
/*
* It might be a bit nicer if this were declared as
* a subclass of LinkedQueue, or a sibling class of
* a common abstract class. It shares much of the
* basic design and bookkeeping fields. But too
* many details differ to make this worth doing.
*/
/**
* Dummy header node of list. The first actual node, if it exists, is always
* at head_.next. After each take, the old first node becomes the head.
**/
protected LinkedNode head_;
/**
* The last node of list. Put() appends to list, so modifies last_
**/
protected LinkedNode last_;
/**
* Helper monitor. Ensures that only one put at a time executes.
**/
protected final Object putGuard_ = new Object();
/**
* Helper monitor. Protects and provides wait queue for takes
**/
protected final Object takeGuard_ = new Object();
/** Number of elements allowed **/
protected int capacity_;
/**
* One side of a split permit count.
* The counts represent permits to do a put. (The queue is full when zero).
* Invariant: putSidePutPermits_ + takeSidePutPermits_ = capacity_ - length.
* (The length is never separately recorded, so this cannot be
* checked explicitly.)
* To minimize contention between puts and takes, the
* put side uses up all of its permits before transfering them from
* the take side. The take side just increments the count upon each take.
* Thus, most puts and take can run independently of each other unless
* the queue is empty or full.
* Initial value is queue capacity.
**/
protected int putSidePutPermits_;
/** Number of takes since last reconcile **/
protected int takeSidePutPermits_ = 0;
/**
* Create a queue with the given capacity
* @exception IllegalArgumentException if capacity less or equal to zero
**/
public BoundedLinkedQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
capacity_ = capacity;
putSidePutPermits_ = capacity;
head_ = new LinkedNode(null);
last_ = head_;
}
/**
* Create a queue with the current default capacity
**/
public BoundedLinkedQueue() {
this(DefaultChannelCapacity.get());
}
/**
* Move put permits from take side to put side;
* return the number of put side permits that are available.
* Call only under synch on puGuard_ AND this.
**/
protected final int reconcilePutPermits() {
putSidePutPermits_ += takeSidePutPermits_;
takeSidePutPermits_ = 0;
return putSidePutPermits_;
}
/** Return the current capacity of this queue **/
public synchronized int capacity() { return capacity_; }
/**
* Return the number of elements in the queue.
* This is only a snapshot value, that may be in the midst
* of changing. The returned value will be unreliable in the presence of
* active puts and takes, and should only be used as a heuristic
* estimate, for example for resource monitoring purposes.
**/
public synchronized int size() {
/*
This should ideally synch on putGuard_, but
doing so would cause it to block waiting for an in-progress
put, which might be stuck. So we instead use whatever
value of putSidePutPermits_ that we happen to read.
*/
return capacity_ - (takeSidePutPermits_ + putSidePutPermits_);
}
/**
* Reset the capacity of this queue.
* If the new capacity is less than the old capacity,
* existing elements are NOT removed, but
* incoming puts will not proceed until the number of elements
* is less than the new capacity.
* @exception IllegalArgumentException if capacity less or equal to zero
**/
public void setCapacity(int newCapacity) {
if (newCapacity <= 0) throw new IllegalArgumentException();
synchronized (putGuard_) {
synchronized(this) {
takeSidePutPermits_ += (newCapacity - capacity_);
capacity_ = newCapacity;
// Force immediate reconcilation.
reconcilePutPermits();
notifyAll();
}
}
}
/** Main mechanics for take/poll **/
protected synchronized Object extract() {
synchronized(head_) {
Object x = null;
LinkedNode first = head_.next;
if (first != null) {
x = first.value;
first.value = null;
head_ = first;
++takeSidePutPermits_;
notify();
}
return x;
}
}
public Object peek() {
synchronized(head_) {
LinkedNode first = head_.next;
if (first != null)
return first.value;
else
return null;
}
}
public Object take() throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
Object x = extract();
if (x != null)
return x;
else {
synchronized(takeGuard_) {
try {
for (;;) {
x = extract();
if (x != null) {
return x;
}
else {
takeGuard_.wait();
}
}
}
catch(InterruptedException ex) {
takeGuard_.notify();
throw ex;
}
}
}
}
public Object poll(long msecs) throws InterruptedException {
if (Thread.interrupted()) throw new InterruptedException();
Object x = extract();
if (x != null)
return x;
else {
synchronized(takeGuard_) {
try {
long waitTime = msecs;
long start = (msecs <= 0)? 0: System.currentTimeMillis();
for (;;) {
x = extract();
if (x != null || waitTime <= 0) {
return x;
}
else {
takeGuard_.wait(waitTime);
waitTime = msecs - (System.currentTimeMillis() - start);
}
}
}
catch(InterruptedException ex) {
takeGuard_.notify();
throw ex;
}
}
}
}
/** Notify a waiting take if needed **/
protected final void allowTake() {
synchronized(takeGuard_) {
takeGuard_.notify();
}
}
/**
* Create and insert a node.
* Call only under synch on putGuard_
**/
protected void insert(Object x) {
--putSidePutPermits_;
LinkedNode p = new LinkedNode(x);
synchronized(last_) {
last_.next = p;
last_ = p;
}
}
/*
put and offer(ms) differ only in policy before insert/allowTake
*/
public void put(Object x) throws InterruptedException {
if (x == null) throw new IllegalArgumentException();
if (Thread.interrupted()) throw new InterruptedException();
synchronized(putGuard_) {
if (putSidePutPermits_ <= 0) { // wait for permit.
synchronized(this) {
if (reconcilePutPermits() <= 0) {
try {
for(;;) {
wait();
if (reconcilePutPermits() > 0) {
break;
}
}
}
catch (InterruptedException ex) {
notify();
throw ex;
}
}
}
}
insert(x);
}
// call outside of lock to loosen put/take coupling
allowTake();
}
public boolean offer(Object x, long msecs) throws InterruptedException {
if (x == null) throw new IllegalArgumentException();
if (Thread.interrupted()) throw new InterruptedException();
synchronized(putGuard_) {
if (putSidePutPermits_ <= 0) {
synchronized(this) {
if (reconcilePutPermits() <= 0) {
if (msecs <= 0)
return false;
else {
try {
long waitTime = msecs;
long start = System.currentTimeMillis();
for(;;) {
wait(waitTime);
if (reconcilePutPermits() > 0) {
break;
}
else {
waitTime = msecs - (System.currentTimeMillis() - start);
if (waitTime <= 0) {
return false;
}
}
}
}
catch (InterruptedException ex) {
notify();
throw ex;
}
}
}
}
}
insert(x);
}
allowTake();
return true;
}
public boolean isEmpty() {
synchronized(head_) {
return head_.next == null;
}
}
}
|