| Matching Statements |
| File1 Line# |
File2 Line# |
Statement |
| 8 | 7 | package java.util.concurrent |
| 9 | 8 | import java.util.concurrent.locks.* |
| 10 | 9 | import java.util.* |
| 47 | 46 | public class ArrayBlockingQueue<E> extends AbstractQueue<E> |
| 48 | 47 | implements BlockingQueue<E>, java.io.Serializable { |
| 56 | 55 | private static final long serialVersionUID = -817911632652898426L |
| 59 | 58 | private final E[] items |
| 65 | 64 | private int count |
| 73 | 72 | private final ReentrantLock lock |
| 75 | 74 | private final Condition notEmpty |
| 77 | 76 | private final Condition notFull |
| 84 | 83 | final int inc(int i) { |
| 85 | 84 | return (++i == items.length)? 0 : i |
| 92 | 91 | private void insert(E x) { |
| 93 | 92 | items[putIndex] = x |
| 94 | 93 | putIndex = inc(putIndex) |
| 95 | 94 | ++count |
96 288 343 | 95 318 343 | notEmpty.signal() |
| 103 | 102 | private E extract() { |
104 118 304 362 422 440 458 498 522 555 | 103 117 245 416 447 478 532 571 601 639 | final E[] items = this.items |
| 105 | 104 | E x = items[takeIndex] |
106 121 | 105 120 | items[takeIndex] = null |
107 122 | 106 121 | takeIndex = inc(takeIndex) |
108 137 | 107 136 | --count |
109 138 250 370 | 108 137 253 288 | notFull.signal() |
110 267 281 347 669 | 109 304 322 336 753 | return x |
| 117 | 116 | void removeAt(int i) { |
| 120 | 119 | if (i == takeIndex) { |
| 126 | 125 | int nexti = inc(i) |
| 127 | 126 | if (nexti != putIndex) { |
| 128 | 127 | items[i] = items[nexti] |
| 129 | 128 | i = nexti |
131 505 531 565 | 130 578 610 649 | items[i] = null |
| 132 | 131 | putIndex = i |
| 147 | 147 | public ArrayBlockingQueue(int capacity) { |
| 148 | 148 | this(capacity, false) |
| 160 | 161 | public ArrayBlockingQueue(int capacity, boolean fair) { |
| 161 | 162 | if (capacity <= 0) |
162 188 521 552 | 163 190 600 636 | throw new IllegalArgumentException() |
| 163 | 164 | this.items = (E[]) new Object[capacity] |
| 164 | 165 | lock = new ReentrantLock(fair) |
| 165 | 166 | notEmpty = lock.newCondition() |
| 166 | 167 | notFull = lock.newCondition() |
| 184 | 186 | public ArrayBlockingQueue(int capacity, boolean fair, |
| 185 | 187 | Collection<? extends E> c) { |
| 186 | 188 | this(capacity, fair) |
| 187 | 189 | if (capacity < c.size()) |
| 190 | 192 | for (Iterator<? extends E> it = c.iterator() |
| 190 | 192 | it.hasNext() |
| 191 | 193 | add(it.next()) |
205 236 261 274 305 326 336 363 387 410 423 441 459 483 499 523 556 592 | 222 246 275 298 311 330 354 371 394 417 448 479 533 557 572 602 640 676 | final ReentrantLock lock = this.lock |
206 262 306 327 388 411 424 442 460 484 500 524 557 593 661 677 | 223 299 355 372 395 418 449 480 534 558 573 603 641 677 745 761 | lock.lock() |
| 208 | 225 | if (count == items.length) |
215 255 269 294 321 331 349 375 392 415 435 453 478 488 513 543 576 597 671 690 | 232 258 293 306 324 349 359 376 399 433 460 491 552 562 586 622 660 681 755 774 | lock.unlock() |
| 233 | 271 | throws InterruptedException { |
237 275 337 364 | 247 276 312 331 | lock.lockInterruptibly() |
239 277 | 274 329 | long nanos = unit.toNanos(timeout) |
| 241 | 279 | if (count != items.length) { |
245 283 | 283 338 | if (nanos <= 0) |
| 248 | 286 | nanos = notFull.awaitNanos(nanos) |
249 287 342 369 | 252 287 317 342 | } catch (InterruptedException ie) { |
251 289 344 371 | 254 289 319 344 | throw ie |
| 260 | 297 | public E poll() { |
264 627 | 301 711 | if (count == 0) |
266 280 346 | 303 321 335 | E x = extract() |
| 273 | 328 | public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
| 279 | 334 | if (count != 0) { |
| 286 | 341 | nanos = notEmpty.awaitNanos(nanos) |
| 302 | 414 | public boolean remove(Object o) { |
303 421 | 415 446 | if (o == null) return false |
308 426 446 469 502 526 559 | 420 451 484 543 575 605 643 | int i = takeIndex |
309 427 445 468 | 421 452 483 542 | int k = 0 |
| 311 | 423 | if (k++ >= count) |
| 313 | 425 | if (o.equals(items[i])) { |
314 685 | 426 769 | removeAt(i) |
317 431 449 472 506 532 566 | 429 456 487 546 579 611 650 | i = inc(i) |
| 325 | 353 | public E peek() { |
| 329 | 357 | return (count == 0) ? null : items[takeIndex] |
| 335 | 310 | public E take() throws InterruptedException { |
| 340 | 315 | while (count == 0) |
| 341 | 316 | notEmpty.await() |
| 367 | 250 | while (count == items.length) |
| 368 | 251 | notFull.await() |
| 386 | 370 | public int size() { |
| 390 | 374 | return count |
| 409 | 393 | public int remainingCapacity() { |
| 413 | 397 | return items.length - count |
| 420 | 445 | public boolean contains(Object o) { |
| 428 | 453 | while (k++ < count) { |
| 429 | 454 | if (o.equals(items[i])) |
| 439 | 477 | public Object[] toArray() { |
| 444 | 482 | Object[] a = new Object[count] |
447 470 | 485 544 | while (k < count) { |
| 448 | 486 | a[k++] = items[i] |
451 476 | 489 550 | return a |
| 457 | 531 | public <T> T[] toArray(T[] a) { |
| 462 | 536 | if (a.length < count) |
| 463 | 537 | a = (T[])java.lang.reflect.Array.newInstance( |
| 464 | 538 | a.getClass().getComponentType(), |
| 465 | 539 | count |
| 471 | 545 | a[k++] = (T)items[i] |
| 474 | 548 | if (a.length > count) |
| 475 | 549 | a[count] = null |
| 482 | 556 | public String toString() { |
| 486 | 560 | return super.toString() |
| 497 | 570 | public void clear() { |
| 503 | 576 | int k = count |
| 504 | 577 | while (k-- > 0) { |
508 536 | 581 615 | count = 0 |
509 537 | 582 616 | putIndex = 0 |
510 538 | 583 617 | takeIndex = 0 |
511 539 572 | 584 618 656 | notFull.signalAll() |
| 517 | 596 | public int drainTo(Collection<? super E> c) { |
518 549 | 597 633 | if (c == null) |
519 550 | 598 634 | throw new NullPointerException() |
520 551 | 599 635 | if (c == this) |
527 560 | 606 644 | int n = 0 |
| 528 | 607 | int max = count |
529 563 | 608 647 | while (n < max) { |
530 564 | 609 648 | c.add(items[i]) |
533 567 | 612 651 | ++n |
535 569 | 614 653 | if (n > 0) { |
541 574 | 620 658 | return n |
| 548 | 632 | public int drainTo(Collection<? super E> c, int maxElements) { |
| 553 | 637 | if (maxElements <= 0) |
| 554 | 638 | return 0 |
| 561 | 645 | int sz = count |
| 562 | 646 | int max = (maxElements < count)? maxElements : count |
| 570 | 654 | count -= n |
| 571 | 655 | takeIndex = i |
| 591 | 675 | public Iterator<E> iterator() { |
| 595 | 679 | return new Itr() |
| 604 | 688 | private class Itr implements Iterator<E> { |
| 609 | 693 | private int nextIndex |
| 617 | 701 | private E nextItem |
| 623 | 707 | private int lastRet |
| 625 | 709 | Itr() { |
626 682 | 710 766 | lastRet = -1 |
628 650 655 | 712 734 739 | nextIndex = -1 |
| 630 | 714 | nextIndex = takeIndex |
| 631 | 715 | nextItem = items[takeIndex] |
| 635 | 719 | public boolean hasNext() { |
| 641 | 725 | return nextIndex >= 0 |
| 648 | 732 | private void checkNext() { |
| 649 | 733 | if (nextIndex == putIndex) { |
| 651 | 735 | nextItem = null |
| 653 | 737 | nextItem = items[nextIndex] |
| 654 | 738 | if (nextItem == null) |
| 659 | 743 | public E next() { |
660 676 | 744 760 | final ReentrantLock lock = ArrayBlockingQueue.this.lock |
| 663 | 747 | if (nextIndex < 0) |
| 664 | 748 | throw new NoSuchElementException() |
| 665 | 749 | lastRet = nextIndex |
| 666 | 750 | E x = nextItem |
| 667 | 751 | nextIndex = inc(nextIndex) |
668 688 | 752 772 | checkNext() |
| 675 | 759 | public void remove() { |
| 679 | 763 | int i = lastRet |
| 680 | 764 | if (i == -1) |
| 681 | 765 | throw new IllegalStateException() |
| 684 | 768 | int ti = takeIndex |
| 687 | 771 | nextIndex = (i == ti) ? takeIndex : i |
| Matching Comments and Strings |
| File1 Line# |
File2 Line# |
Comment/String |
| 13 | 16 | * A bounded {@linkplain BlockingQueue blocking queue} backed by an |
| 14 | 17 | * array. This queue orders elements FIFO (first-in-first-out). The |
| 15 | 18 | * <em>head</em> of the queue is that element that has been on the |
| 16 | 19 | * queue the longest time. The <em>tail</em> of the queue is that |
| 17 | 20 | * element that has been on the queue the shortest time. New elements |
| 18 | 21 | * are inserted at the tail of the queue, and the queue retrieval |
| 19 | 22 | * operations obtain elements at the head of the queue. |
| 21 | 24 | * <p>This is a classic "bounded buffer", in which a |
| 22 | 25 | * fixed-sized array holds elements inserted by producers and |
| 23 | 26 | * extracted by consumers. Once created, the capacity cannot be |
| 26 | 29 | * element from an empty queue will similarly block. |
| 28 | 31 | * <p> This class supports an optional fairness policy for ordering |
| 29 | 32 | * waiting producer and consumer threads. By default, this ordering |
| 30 | 33 | * is not guaranteed. However, a queue constructed with fairness set |
| 31 | 34 | * to <tt>true</tt> grants threads access in FIFO order. Fairness |
| 32 | 35 | * generally decreases throughput but reduces variability and avoids |
| 33 | 36 | * starvation. |
| 35 | 38 | * <p>This class and its iterator implement all of the |
| 36 | 39 | * <em>optional</em> methods of the {@link Collection} and {@link |
| 37 | 40 | * Iterator} interfaces. |
| 43 | 42 | * @since 1.5 |
| 44 | 43 | * @author Doug Lea |
| 45 | 44 | * @param <E> the type of elements held in this collection |
| 51 | 50 | * Serialization ID. This class relies on default serialization |
| 52 | 51 | * even for the items array, which is default-serialized, even if |
| 53 | 52 | * it is empty. Otherwise it could not be declared final, which is |
| 54 | 53 | * necessary here. |
| 58 | 57 | * The queued items |
| 60 | 59 | * items index for next take, poll or remove |
| 62 | 61 | * items index for next put, offer, or add. |
| 64 | 63 | * Number of items in the queue |
| 68 | 67 | * Concurrency control uses the classic two-condition algorithm |
| 69 | 68 | * found in any textbook. |
| 72 | 71 | * Main lock guarding all access |
| 74 | 73 | * Condition for waiting takes |
| 76 | 75 | * Condition for waiting puts |
| 79 | 78 | Internal helper methods |
| 82 | 81 | * Circularly increment i. |
90 101 115 | 89 100 114 | * Call only when holding lock. |
| 114 | 113 | * Utility for remove and iterator.remove: Delete item at position i. |
| 119 | 118 | if removing front item, just advance |
| 124 | 123 | slide over all others up through putIndex. |
142 152 170 | 141 152 171 | * Creates an <tt>ArrayBlockingQueue</tt> with the given (fixed) |
| 143 | 142 | * capacity and default access policy. |
144 154 174 | 144 155 176 | * @param capacity the capacity of this queue |
145 158 | 145 159 | * @throws IllegalArgumentException if <tt>capacity</tt> is less than 1 |
| 153 | 153 | * capacity and the specified access policy. |
155 175 | 156 177 | * @param fair if <tt>true</tt> then queue accesses for threads blocked |
| 171 | 172 | * capacity, the specified access policy and initially containing the |
| 172 | 173 | * elements of the given collection, |
| 173 | 174 | * added in traversal order of the collection's iterator. |
| 178 | 180 | * @param c the collection of elements to initially contain |
| 179 | 181 | * @throws IllegalArgumentException if <tt>capacity</tt> is less than |
| 180 | 182 | * <tt>c.size()</tt>, or less than 1. |
250 288 343 370 | 253 288 318 343 | propagate to non-interrupted thread |
| 379 | 363 | this doc comment is overridden to remove the reference to collections |
| 380 | 364 | greater in size than Integer.MAX_VALUE |
| 382 | 366 | * Returns the number of elements in this queue. |
| 396 | 380 | this doc comment is a modified copy of the inherited doc comment, |
| 397 | 381 | without the reference to unlimited queues. |
| 401 | 385 | * blocking. This is always equal to the initial capacity of this queue |
| 402 | 386 | * less the current <tt>size</tt> of this queue. |
| 494 | 567 | * Atomically removes all of the elements from this queue. |
| 495 | 568 | * The queue will be empty after this call returns. |
| 582 | 666 | * Returns an iterator over the elements in this queue in proper sequence. |
| 583 | 667 | * The returned <tt>Iterator</tt> is a "weakly consistent" iterator that |
| 585 | 669 | * and guarantees to traverse elements as they existed upon |
| 586 | 670 | * construction of the iterator, and may (but is not guaranteed to) |
| 587 | 671 | * reflect any modifications subsequent to construction. |
| 602 | 686 | * Iterator for ArrayBlockingQueue |
| 606 | 690 | * Index of element to be returned by next, |
| 607 | 691 | * or a negative number if no such. |
| 612 | 696 | * nextItem holds on to item fields because once we claim |
| 613 | 697 | * that an element exists in hasNext(), we must return it in |
| 614 | 698 | * the following next() call even if it was in the process of |
| 615 | 699 | * being removed when hasNext() was called. |
| 620 | 704 | * Index of element returned by most recent call to next. |
| 621 | 705 | * Reset to -1 if this element is deleted by a call to remove. |
| 637 | 721 | * No sync. We can return true by mistake here |
| 638 | 722 | * only if this iterator passed across threads, |
| 639 | 723 | * which we don't support anyway. |
| 646 | 730 | * Stops iterator when either hits putIndex or sees null item. |
| 686 | 770 | back up cursor (reset to front if was first element) |