10 |
|
|
11 |
|
import java.util.AbstractQueue; |
12 |
|
import java.util.Collection; |
13 |
+ |
import java.util.ConcurrentModificationException; |
14 |
|
import java.util.Iterator; |
15 |
|
import java.util.NoSuchElementException; |
16 |
+ |
import java.util.Queue; |
17 |
|
import java.util.concurrent.locks.LockSupport; |
18 |
|
import java.util.concurrent.atomic.AtomicReference; |
19 |
|
|
20 |
|
/** |
21 |
< |
* An unbounded {@linkplain TransferQueue} based on linked nodes. |
21 |
> |
* An unbounded {@link TransferQueue} based on linked nodes. |
22 |
|
* This queue orders elements FIFO (first-in-first-out) with respect |
23 |
|
* to any given producer. The <em>head</em> of the queue is that |
24 |
|
* element that has been on the queue the longest time for some |
294 |
|
* @param e the comparison value for checking match |
295 |
|
* @param mode mode |
296 |
|
* @param nanos timeout value |
297 |
< |
* @return matched item, or s if cancelled |
297 |
> |
* @return matched item, or null if cancelled |
298 |
|
*/ |
299 |
|
private E awaitFulfill(Node<E> pred, Node<E> s, E e, |
300 |
|
int mode, long nanos) { |
360 |
|
for (;;) { |
361 |
|
Node<E> h = head.get(); |
362 |
|
Node<E> first = h.next; |
363 |
< |
if (first != null && first.next == first) { // help advance |
363 |
> |
if (first != null && first.get() == first) { // help advance |
364 |
|
advanceHead(h, first); |
365 |
|
continue; |
366 |
|
} |
473 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
< |
* @throws InterruptedException {@inheritDoc} |
477 |
< |
* @throws NullPointerException {@inheritDoc} |
476 |
> |
* Inserts the specified element at the tail of this queue. |
477 |
> |
* As the queue is unbounded, this method will never block. |
478 |
> |
* |
479 |
> |
* @throws NullPointerException if the specified element is null |
480 |
|
*/ |
481 |
< |
public void put(E e) throws InterruptedException { |
482 |
< |
if (e == null) throw new NullPointerException(); |
479 |
< |
if (Thread.interrupted()) throw new InterruptedException(); |
480 |
< |
xfer(e, NOWAIT, 0); |
481 |
> |
public void put(E e) { |
482 |
> |
offer(e); |
483 |
|
} |
484 |
|
|
485 |
|
/** |
486 |
< |
* @throws InterruptedException {@inheritDoc} |
487 |
< |
* @throws NullPointerException {@inheritDoc} |
486 |
> |
* Inserts the specified element at the tail of this queue. |
487 |
> |
* As the queue is unbounded, this method will never block or |
488 |
> |
* return {@code false}. |
489 |
> |
* |
490 |
> |
* @return {@code true} (as specified by |
491 |
> |
* {@link BlockingQueue#offer(Object,long,TimeUnit) BlockingQueue.offer}) |
492 |
> |
* @throws NullPointerException if the specified element is null |
493 |
|
*/ |
494 |
< |
public boolean offer(E e, long timeout, TimeUnit unit) |
495 |
< |
throws InterruptedException { |
489 |
< |
if (e == null) throw new NullPointerException(); |
490 |
< |
if (Thread.interrupted()) throw new InterruptedException(); |
491 |
< |
xfer(e, NOWAIT, 0); |
492 |
< |
return true; |
494 |
> |
public boolean offer(E e, long timeout, TimeUnit unit) { |
495 |
> |
return offer(e); |
496 |
|
} |
497 |
|
|
498 |
|
/** |
499 |
< |
* @throws NullPointerException {@inheritDoc} |
499 |
> |
* Inserts the specified element at the tail of this queue. |
500 |
> |
* As the queue is unbounded, this method will never return {@code false}. |
501 |
> |
* |
502 |
> |
* @return {@code true} (as specified by |
503 |
> |
* {@link BlockingQueue#offer(Object) BlockingQueue.offer}) |
504 |
> |
* @throws NullPointerException if the specified element is null |
505 |
|
*/ |
506 |
|
public boolean offer(E e) { |
507 |
|
if (e == null) throw new NullPointerException(); |
510 |
|
} |
511 |
|
|
512 |
|
/** |
513 |
< |
* @throws NullPointerException {@inheritDoc} |
513 |
> |
* Inserts the specified element at the tail of this queue. |
514 |
> |
* As the queue is unbounded, this method will never throw |
515 |
> |
* {@link IllegalStateException} or return {@code false}. |
516 |
> |
* |
517 |
> |
* @return {@code true} (as specified by {@link Collection#add}) |
518 |
> |
* @throws NullPointerException if the specified element is null |
519 |
|
*/ |
520 |
|
public boolean add(E e) { |
521 |
+ |
return offer(e); |
522 |
+ |
} |
523 |
+ |
|
524 |
+ |
/** |
525 |
+ |
* Transfers the element to a waiting consumer immediately, if possible. |
526 |
+ |
* |
527 |
+ |
* <p>More precisely, transfers the specified element immediately |
528 |
+ |
* if there exists a consumer already waiting to receive it (in |
529 |
+ |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
530 |
+ |
* otherwise returning {@code false} without enqueuing the element. |
531 |
+ |
* |
532 |
+ |
* @throws NullPointerException if the specified element is null |
533 |
+ |
*/ |
534 |
+ |
public boolean tryTransfer(E e) { |
535 |
|
if (e == null) throw new NullPointerException(); |
536 |
< |
xfer(e, NOWAIT, 0); |
510 |
< |
return true; |
536 |
> |
return fulfill(e) != null; |
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
< |
* @throws InterruptedException {@inheritDoc} |
541 |
< |
* @throws NullPointerException {@inheritDoc} |
540 |
> |
* Transfers the element to a consumer, waiting if necessary to do so. |
541 |
> |
* |
542 |
> |
* <p>More precisely, transfers the specified element immediately |
543 |
> |
* if there exists a consumer already waiting to receive it (in |
544 |
> |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
545 |
> |
* else inserts the specified element at the tail of this queue |
546 |
> |
* and waits until the element is received by a consumer. |
547 |
> |
* |
548 |
> |
* @throws NullPointerException if the specified element is null |
549 |
|
*/ |
550 |
|
public void transfer(E e) throws InterruptedException { |
551 |
|
if (e == null) throw new NullPointerException(); |
556 |
|
} |
557 |
|
|
558 |
|
/** |
559 |
< |
* @throws InterruptedException {@inheritDoc} |
560 |
< |
* @throws NullPointerException {@inheritDoc} |
559 |
> |
* Transfers the element to a consumer if it is possible to do so |
560 |
> |
* before the timeout elapses. |
561 |
> |
* |
562 |
> |
* <p>More precisely, transfers the specified element immediately |
563 |
> |
* if there exists a consumer already waiting to receive it (in |
564 |
> |
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}), |
565 |
> |
* else inserts the specified element at the tail of this queue |
566 |
> |
* and waits until the element is received by a consumer, |
567 |
> |
* returning {@code false} if the specified wait time elapses |
568 |
> |
* before the element can be transferred. |
569 |
> |
* |
570 |
> |
* @throws NullPointerException if the specified element is null |
571 |
|
*/ |
572 |
|
public boolean tryTransfer(E e, long timeout, TimeUnit unit) |
573 |
|
throws InterruptedException { |
579 |
|
throw new InterruptedException(); |
580 |
|
} |
581 |
|
|
539 |
– |
/** |
540 |
– |
* @throws NullPointerException {@inheritDoc} |
541 |
– |
*/ |
542 |
– |
public boolean tryTransfer(E e) { |
543 |
– |
if (e == null) throw new NullPointerException(); |
544 |
– |
return fulfill(e) != null; |
545 |
– |
} |
546 |
– |
|
547 |
– |
/** |
548 |
– |
* @throws InterruptedException {@inheritDoc} |
549 |
– |
*/ |
582 |
|
public E take() throws InterruptedException { |
583 |
< |
Object e = xfer(null, WAIT, 0); |
583 |
> |
E e = xfer(null, WAIT, 0); |
584 |
|
if (e != null) |
585 |
< |
return (E) e; |
585 |
> |
return e; |
586 |
|
Thread.interrupted(); |
587 |
|
throw new InterruptedException(); |
588 |
|
} |
589 |
|
|
558 |
– |
/** |
559 |
– |
* @throws InterruptedException {@inheritDoc} |
560 |
– |
*/ |
590 |
|
public E poll(long timeout, TimeUnit unit) throws InterruptedException { |
591 |
< |
Object e = xfer(null, TIMEOUT, unit.toNanos(timeout)); |
591 |
> |
E e = xfer(null, TIMEOUT, unit.toNanos(timeout)); |
592 |
|
if (e != null || !Thread.interrupted()) |
593 |
< |
return (E) e; |
593 |
> |
return e; |
594 |
|
throw new InterruptedException(); |
595 |
|
} |
596 |
|
|
664 |
|
} |
665 |
|
} |
666 |
|
|
667 |
< |
|
667 |
> |
/** |
668 |
> |
* Returns an iterator over the elements in this queue in proper |
669 |
> |
* sequence, from head to tail. |
670 |
> |
* |
671 |
> |
* <p>The returned iterator is a "weakly consistent" iterator that |
672 |
> |
* will never throw |
673 |
> |
* {@link ConcurrentModificationException ConcurrentModificationException}, |
674 |
> |
* and guarantees to traverse elements as they existed upon |
675 |
> |
* construction of the iterator, and may (but is not guaranteed |
676 |
> |
* to) reflect any modifications subsequent to construction. |
677 |
> |
* |
678 |
> |
* @return an iterator over the elements in this queue in proper sequence |
679 |
> |
*/ |
680 |
|
public Iterator<E> iterator() { |
681 |
|
return new Itr(); |
682 |
|
} |
691 |
|
class Itr implements Iterator<E> { |
692 |
|
Node<E> next; // node to return next |
693 |
|
Node<E> pnext; // predecessor of next |
653 |
– |
Node<E> snext; // successor of next |
694 |
|
Node<E> curr; // last returned node, for remove() |
695 |
|
Node<E> pcurr; // predecessor of curr, for remove() |
696 |
< |
E nextItem; // Cache of next item, once committed to in next |
696 |
> |
E nextItem; // Cache of next item, once committed to in next |
697 |
|
|
698 |
|
Itr() { |
699 |
< |
findNext(); |
699 |
> |
advance(); |
700 |
|
} |
701 |
|
|
702 |
|
/** |
703 |
< |
* Ensures next points to next valid node, or null if none. |
703 |
> |
* Moves to next valid node and returns item to return for |
704 |
> |
* next(), or null if no such. |
705 |
|
*/ |
706 |
< |
void findNext() { |
706 |
> |
private E advance() { |
707 |
> |
pcurr = pnext; |
708 |
> |
curr = next; |
709 |
> |
E item = nextItem; |
710 |
> |
|
711 |
|
for (;;) { |
712 |
< |
Node<E> pred = pnext; |
713 |
< |
Node<E> q = next; |
714 |
< |
if (pred == null || pred == q) { |
670 |
< |
pred = traversalHead(); |
671 |
< |
q = pred.next; |
672 |
< |
} |
673 |
< |
if (q == null || !q.isData) { |
712 |
> |
pnext = (next == null) ? traversalHead() : next; |
713 |
> |
next = pnext.next; |
714 |
> |
if (next == pnext) { |
715 |
|
next = null; |
716 |
< |
return; |
716 |
> |
continue; // restart |
717 |
|
} |
718 |
< |
Object x = q.get(); |
719 |
< |
Node<E> s = q.next; |
720 |
< |
if (x != null && q != x && q != s) { |
718 |
> |
if (next == null) |
719 |
> |
break; |
720 |
> |
Object x = next.get(); |
721 |
> |
if (x != null && x != next) { |
722 |
|
nextItem = (E) x; |
723 |
< |
snext = s; |
682 |
< |
pnext = pred; |
683 |
< |
next = q; |
684 |
< |
return; |
723 |
> |
break; |
724 |
|
} |
686 |
– |
pnext = q; |
687 |
– |
next = s; |
725 |
|
} |
726 |
+ |
return item; |
727 |
|
} |
728 |
|
|
729 |
|
public boolean hasNext() { |
731 |
|
} |
732 |
|
|
733 |
|
public E next() { |
734 |
< |
if (next == null) throw new NoSuchElementException(); |
735 |
< |
pcurr = pnext; |
736 |
< |
curr = next; |
699 |
< |
pnext = next; |
700 |
< |
next = snext; |
701 |
< |
E x = nextItem; |
702 |
< |
findNext(); |
703 |
< |
return x; |
734 |
> |
if (next == null) |
735 |
> |
throw new NoSuchElementException(); |
736 |
> |
return advance(); |
737 |
|
} |
738 |
|
|
739 |
|
public void remove() { |
762 |
|
} |
763 |
|
} |
764 |
|
|
765 |
+ |
/** |
766 |
+ |
* Returns {@code true} if this queue contains no elements. |
767 |
+ |
* |
768 |
+ |
* @return {@code true} if this queue contains no elements |
769 |
+ |
*/ |
770 |
|
public boolean isEmpty() { |
771 |
|
for (;;) { |
772 |
|
Node<E> h = traversalHead(); |
808 |
|
* @return the number of elements in this queue |
809 |
|
*/ |
810 |
|
public int size() { |
811 |
< |
int count = 0; |
812 |
< |
Node<E> h = traversalHead(); |
813 |
< |
for (Node<E> p = h.next; p != null && p.isData; p = p.next) { |
814 |
< |
Object x = p.get(); |
815 |
< |
if (x != null && x != p) { |
816 |
< |
if (++count == Integer.MAX_VALUE) // saturated |
811 |
> |
for (;;) { |
812 |
> |
int count = 0; |
813 |
> |
Node<E> pred = traversalHead(); |
814 |
> |
for (;;) { |
815 |
> |
Node<E> q = pred.next; |
816 |
> |
if (q == pred) // restart |
817 |
|
break; |
818 |
+ |
if (q == null || !q.isData) |
819 |
+ |
return count; |
820 |
+ |
Object x = q.get(); |
821 |
+ |
if (x != null && x != q) { |
822 |
+ |
if (++count == Integer.MAX_VALUE) // saturated |
823 |
+ |
return count; |
824 |
+ |
} |
825 |
+ |
pred = q; |
826 |
|
} |
827 |
|
} |
782 |
– |
return count; |
828 |
|
} |
829 |
|
|
830 |
|
public int getWaitingConsumerCount() { |
831 |
< |
int count = 0; |
832 |
< |
Node<E> h = traversalHead(); |
833 |
< |
for (Node<E> p = h.next; p != null && !p.isData; p = p.next) { |
834 |
< |
if (p.get() == null) { |
835 |
< |
if (++count == Integer.MAX_VALUE) |
831 |
> |
// converse of size -- count valid non-data nodes |
832 |
> |
for (;;) { |
833 |
> |
int count = 0; |
834 |
> |
Node<E> pred = traversalHead(); |
835 |
> |
for (;;) { |
836 |
> |
Node<E> q = pred.next; |
837 |
> |
if (q == pred) // restart |
838 |
|
break; |
839 |
+ |
if (q == null || q.isData) |
840 |
+ |
return count; |
841 |
+ |
Object x = q.get(); |
842 |
+ |
if (x == null) { |
843 |
+ |
if (++count == Integer.MAX_VALUE) // saturated |
844 |
+ |
return count; |
845 |
+ |
} |
846 |
+ |
pred = q; |
847 |
|
} |
848 |
|
} |
794 |
– |
return count; |
795 |
– |
} |
796 |
– |
|
797 |
– |
public int remainingCapacity() { |
798 |
– |
return Integer.MAX_VALUE; |
849 |
|
} |
850 |
|
|
851 |
+ |
/** |
852 |
+ |
* Removes a single instance of the specified element from this queue, |
853 |
+ |
* if it is present. More formally, removes an element {@code e} such |
854 |
+ |
* that {@code o.equals(e)}, if this queue contains one or more such |
855 |
+ |
* elements. |
856 |
+ |
* Returns {@code true} if this queue contained the specified element |
857 |
+ |
* (or equivalently, if this queue changed as a result of the call). |
858 |
+ |
* |
859 |
+ |
* @param o element to be removed from this queue, if present |
860 |
+ |
* @return {@code true} if this queue changed as a result of the call |
861 |
+ |
*/ |
862 |
|
public boolean remove(Object o) { |
863 |
|
if (o == null) |
864 |
|
return false; |
866 |
|
Node<E> pred = traversalHead(); |
867 |
|
for (;;) { |
868 |
|
Node<E> q = pred.next; |
808 |
– |
if (q == null || !q.isData) |
809 |
– |
return false; |
869 |
|
if (q == pred) // restart |
870 |
|
break; |
871 |
+ |
if (q == null || !q.isData) |
872 |
+ |
return false; |
873 |
|
Object x = q.get(); |
874 |
|
if (x != null && x != q && o.equals(x) && |
875 |
|
q.compareAndSet(x, q)) { |
882 |
|
} |
883 |
|
|
884 |
|
/** |
885 |
+ |
* Always returns {@code Integer.MAX_VALUE} because a |
886 |
+ |
* {@code LinkedTransferQueue} is not capacity constrained. |
887 |
+ |
* |
888 |
+ |
* @return {@code Integer.MAX_VALUE} (as specified by |
889 |
+ |
* {@link BlockingQueue#remainingCapacity()}) |
890 |
+ |
*/ |
891 |
+ |
public int remainingCapacity() { |
892 |
+ |
return Integer.MAX_VALUE; |
893 |
+ |
} |
894 |
+ |
|
895 |
+ |
/** |
896 |
|
* Save the state to a stream (that is, serialize it). |
897 |
|
* |
898 |
|
* @serialData All of the elements (each an {@code E}) in |