492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
+ |
public void clear() { |
496 |
+ |
fullyLock(); |
497 |
+ |
try { |
498 |
+ |
head.next = null; |
499 |
+ |
if (count.getAndSet(0) == capacity) |
500 |
+ |
notFull.signalAll(); |
501 |
+ |
} finally { |
502 |
+ |
fullyUnlock(); |
503 |
+ |
} |
504 |
+ |
} |
505 |
+ |
|
506 |
+ |
public int drainTo(Collection<? super E> c) { |
507 |
+ |
if (c == null) |
508 |
+ |
throw new NullPointerException(); |
509 |
+ |
if (c == this) |
510 |
+ |
throw new IllegalArgumentException(); |
511 |
+ |
Node first; |
512 |
+ |
fullyLock(); |
513 |
+ |
try { |
514 |
+ |
first = head.next; |
515 |
+ |
head.next = null; |
516 |
+ |
if (count.getAndSet(0) == capacity) |
517 |
+ |
notFull.signalAll(); |
518 |
+ |
} finally { |
519 |
+ |
fullyUnlock(); |
520 |
+ |
} |
521 |
+ |
// Transfer the elements outside of locks |
522 |
+ |
int n = 0; |
523 |
+ |
for (Node p = first; p != null; p = p.next) { |
524 |
+ |
c.add((E)p.item); |
525 |
+ |
p.item = null; |
526 |
+ |
++n; |
527 |
+ |
} |
528 |
+ |
return n; |
529 |
+ |
} |
530 |
+ |
|
531 |
+ |
public int drainTo(Collection<? super E> c, int maxElements) { |
532 |
+ |
if (c == null) |
533 |
+ |
throw new NullPointerException(); |
534 |
+ |
if (c == this) |
535 |
+ |
throw new IllegalArgumentException(); |
536 |
+ |
if (maxElements <= 0) |
537 |
+ |
return 0; |
538 |
+ |
fullyLock(); |
539 |
+ |
try { |
540 |
+ |
int n = 0; |
541 |
+ |
Node p = head.next; |
542 |
+ |
while (p != null && n < maxElements) { |
543 |
+ |
c.add((E)p.item); |
544 |
+ |
p.item = null; |
545 |
+ |
p = p.next; |
546 |
+ |
++n; |
547 |
+ |
} |
548 |
+ |
if (n != 0) { |
549 |
+ |
head.next = p; |
550 |
+ |
if (count.getAndAdd(-n) == capacity) |
551 |
+ |
notFull.signalAll(); |
552 |
+ |
} |
553 |
+ |
return n; |
554 |
+ |
} finally { |
555 |
+ |
fullyUnlock(); |
556 |
+ |
} |
557 |
+ |
} |
558 |
+ |
|
559 |
+ |
|
560 |
+ |
|
561 |
|
/** |
562 |
|
* Returns an iterator over the elements in this queue in proper sequence. |
563 |
|
* The returned <tt>Iterator</tt> is a "weakly consistent" iterator that |