ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/jsr166y/LinkedTransferQueue.java
(Generate patch)

Comparing jsr166/src/jsr166y/LinkedTransferQueue.java (file contents):
Revision 1.33 by dl, Thu Jul 30 13:30:19 2009 UTC vs.
Revision 1.36 by jsr166, Fri Jul 31 07:30:29 2009 UTC

# Line 10 | Line 10 | import java.util.concurrent.*;
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  
# Line 471 | Line 473 | public class LinkedTransferQueue<E> exte
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();
# Line 502 | Line 510 | public class LinkedTransferQueue<E> exte
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 specified element immediately if there exists a
526 +     * consumer already waiting to receive it (in {@link #take} or
527 +     * timed {@link #poll(long,TimeUnit) poll}), otherwise
528 +     * returning {@code false} without enqueuing the element.
529 +     *
530 +     * @throws NullPointerException if the specified element is null
531 +     */
532 +    public boolean tryTransfer(E e) {
533          if (e == null) throw new NullPointerException();
534 <        xfer(e, NOWAIT, 0);
510 <        return true;
534 >        return fulfill(e) != null;
535      }
536  
537      /**
538 <     * @throws InterruptedException {@inheritDoc}
539 <     * @throws NullPointerException {@inheritDoc}
538 >     * Inserts the specified element at the tail of this queue,
539 >     * waiting if necessary for the element to be received by a
540 >     * consumer invoking {@code take} or {@code poll}.
541 >     *
542 >     * @throws NullPointerException if the specified element is null
543       */
544      public void transfer(E e) throws InterruptedException {
545          if (e == null) throw new NullPointerException();
# Line 523 | Line 550 | public class LinkedTransferQueue<E> exte
550      }
551  
552      /**
553 <     * @throws InterruptedException {@inheritDoc}
554 <     * @throws NullPointerException {@inheritDoc}
553 >     * Inserts the specified element at the tail of this queue,
554 >     * waiting up to the specified wait time for the element to be
555 >     * received by a consumer invoking {@code take} or {@code poll}.
556 >     *
557 >     * @throws NullPointerException if the specified element is null
558       */
559      public boolean tryTransfer(E e, long timeout, TimeUnit unit)
560          throws InterruptedException {
# Line 536 | Line 566 | public class LinkedTransferQueue<E> exte
566          throw new InterruptedException();
567      }
568  
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     */
569      public E take() throws InterruptedException {
570 <        Object e = xfer(null, WAIT, 0);
570 >        E e = xfer(null, WAIT, 0);
571          if (e != null)
572 <            return (E) e;
572 >            return e;
573          Thread.interrupted();
574          throw new InterruptedException();
575      }
576  
558    /**
559     * @throws InterruptedException {@inheritDoc}
560     */
577      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
578 <        Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
578 >        E e = xfer(null, TIMEOUT, unit.toNanos(timeout));
579          if (e != null || !Thread.interrupted())
580 <            return (E) e;
580 >            return e;
581          throw new InterruptedException();
582      }
583  
# Line 635 | Line 651 | public class LinkedTransferQueue<E> exte
651          }
652      }
653  
654 +    /**
655 +     * Returns an iterator over the elements in this queue in proper
656 +     * sequence, from head to tail.
657 +     *
658 +     * <p>The returned iterator is a "weakly consistent" iterator that
659 +     * will never throw
660 +     * {@link ConcurrentModificationException ConcurrentModificationException},
661 +     * and guarantees to traverse elements as they existed upon
662 +     * construction of the iterator, and may (but is not guaranteed
663 +     * to) reflect any modifications subsequent to construction.
664 +     *
665 +     * @return an iterator over the elements in this queue in proper sequence
666 +     */
667      public Iterator<E> iterator() {
668          return new Itr();
669      }
# Line 667 | Line 696 | public class LinkedTransferQueue<E> exte
696              E item = nextItem;
697  
698              for (;;) {
699 <                pnext = next == null ? traversalHead() : next;
699 >                pnext = (next == null) ? traversalHead() : next;
700                  next = pnext.next;
701                  if (next == pnext) {
702                      next = null;
# Line 689 | Line 718 | public class LinkedTransferQueue<E> exte
718          }
719  
720          public E next() {
721 <            if (next == null)
721 >            if (next == null)
722                  throw new NoSuchElementException();
723              return advance();
724          }
# Line 823 | Line 852 | public class LinkedTransferQueue<E> exte
852          }
853      }
854  
855 +    /**
856 +     * Always returns {@code Integer.MAX_VALUE} because a
857 +     * {@code LinkedTransferQueue} is not capacity constrained.
858 +     *
859 +     * @return {@code Integer.MAX_VALUE} (as specified by
860 +     *         {@link BlockingQueue#remainingCapacity()})
861 +     */
862      public int remainingCapacity() {
863          return Integer.MAX_VALUE;
864      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines