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.38 by jsr166, Fri Jul 31 14:48:18 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 if necessary for the
555 >     * element to be received by a consumer invoking {@code take} or
556 >     * {@code poll}.
557 >     *
558 >     * @throws NullPointerException if the specified element is null
559       */
560      public boolean tryTransfer(E e, long timeout, TimeUnit unit)
561          throws InterruptedException {
# Line 536 | Line 567 | public class LinkedTransferQueue<E> exte
567          throw new InterruptedException();
568      }
569  
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     */
570      public E take() throws InterruptedException {
571 <        Object e = xfer(null, WAIT, 0);
571 >        E e = xfer(null, WAIT, 0);
572          if (e != null)
573 <            return (E) e;
573 >            return e;
574          Thread.interrupted();
575          throw new InterruptedException();
576      }
577  
558    /**
559     * @throws InterruptedException {@inheritDoc}
560     */
578      public E poll(long timeout, TimeUnit unit) throws InterruptedException {
579 <        Object e = xfer(null, TIMEOUT, unit.toNanos(timeout));
579 >        E e = xfer(null, TIMEOUT, unit.toNanos(timeout));
580          if (e != null || !Thread.interrupted())
581 <            return (E) e;
581 >            return e;
582          throw new InterruptedException();
583      }
584  
# Line 635 | Line 652 | public class LinkedTransferQueue<E> exte
652          }
653      }
654  
655 +    /**
656 +     * Returns an iterator over the elements in this queue in proper
657 +     * sequence, from head to tail.
658 +     *
659 +     * <p>The returned iterator is a "weakly consistent" iterator that
660 +     * will never throw
661 +     * {@link ConcurrentModificationException ConcurrentModificationException},
662 +     * and guarantees to traverse elements as they existed upon
663 +     * construction of the iterator, and may (but is not guaranteed
664 +     * to) reflect any modifications subsequent to construction.
665 +     *
666 +     * @return an iterator over the elements in this queue in proper sequence
667 +     */
668      public Iterator<E> iterator() {
669          return new Itr();
670      }
# Line 667 | Line 697 | public class LinkedTransferQueue<E> exte
697              E item = nextItem;
698  
699              for (;;) {
700 <                pnext = next == null ? traversalHead() : next;
700 >                pnext = (next == null) ? traversalHead() : next;
701                  next = pnext.next;
702                  if (next == pnext) {
703                      next = null;
# Line 689 | Line 719 | public class LinkedTransferQueue<E> exte
719          }
720  
721          public E next() {
722 <            if (next == null)
722 >            if (next == null)
723                  throw new NoSuchElementException();
724              return advance();
725          }
# Line 823 | Line 853 | public class LinkedTransferQueue<E> exte
853          }
854      }
855  
856 +    /**
857 +     * Always returns {@code Integer.MAX_VALUE} because a
858 +     * {@code LinkedTransferQueue} is not capacity constrained.
859 +     *
860 +     * @return {@code Integer.MAX_VALUE} (as specified by
861 +     *         {@link BlockingQueue#remainingCapacity()})
862 +     */
863      public int remainingCapacity() {
864          return Integer.MAX_VALUE;
865      }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines