equal
deleted
inserted
replaced
71 * Java Collections Framework</a>. |
71 * Java Collections Framework</a>. |
72 * |
72 * |
73 * @since 1.5 |
73 * @since 1.5 |
74 * @author Doug Lea |
74 * @author Doug Lea |
75 * @param <E> the type of elements held in this collection |
75 * @param <E> the type of elements held in this collection |
76 * |
|
77 */ |
76 */ |
78 public class LinkedBlockingQueue<E> extends AbstractQueue<E> |
77 public class LinkedBlockingQueue<E> extends AbstractQueue<E> |
79 implements BlockingQueue<E>, java.io.Serializable { |
78 implements BlockingQueue<E>, java.io.Serializable { |
80 private static final long serialVersionUID = -6903933977591709194L; |
79 private static final long serialVersionUID = -6903933977591709194L; |
81 |
80 |
133 |
132 |
134 /** The capacity bound, or Integer.MAX_VALUE if none */ |
133 /** The capacity bound, or Integer.MAX_VALUE if none */ |
135 private final int capacity; |
134 private final int capacity; |
136 |
135 |
137 /** Current number of elements */ |
136 /** Current number of elements */ |
138 private final AtomicInteger count = new AtomicInteger(0); |
137 private final AtomicInteger count = new AtomicInteger(); |
139 |
138 |
140 /** |
139 /** |
141 * Head of linked list. |
140 * Head of linked list. |
142 * Invariant: head.item == null |
141 * Invariant: head.item == null |
143 */ |
142 */ |
144 private transient Node<E> head; |
143 transient Node<E> head; |
145 |
144 |
146 /** |
145 /** |
147 * Tail of linked list. |
146 * Tail of linked list. |
148 * Invariant: last.next == null |
147 * Invariant: last.next == null |
149 */ |
148 */ |
289 } finally { |
288 } finally { |
290 putLock.unlock(); |
289 putLock.unlock(); |
291 } |
290 } |
292 } |
291 } |
293 |
292 |
294 |
|
295 // this doc comment is overridden to remove the reference to collections |
293 // this doc comment is overridden to remove the reference to collections |
296 // greater in size than Integer.MAX_VALUE |
294 // greater in size than Integer.MAX_VALUE |
297 /** |
295 /** |
298 * Returns the number of elements in this queue. |
296 * Returns the number of elements in this queue. |
299 * |
297 * |
428 if (c == 0) |
426 if (c == 0) |
429 signalNotEmpty(); |
427 signalNotEmpty(); |
430 return c >= 0; |
428 return c >= 0; |
431 } |
429 } |
432 |
430 |
433 |
|
434 public E take() throws InterruptedException { |
431 public E take() throws InterruptedException { |
435 E x; |
432 E x; |
436 int c = -1; |
433 int c = -1; |
437 final AtomicInteger count = this.count; |
434 final AtomicInteger count = this.count; |
438 final ReentrantLock takeLock = this.takeLock; |
435 final ReentrantLock takeLock = this.takeLock; |
628 * |
625 * |
629 * <p>Suppose {@code x} is a queue known to contain only strings. |
626 * <p>Suppose {@code x} is a queue known to contain only strings. |
630 * The following code can be used to dump the queue into a newly |
627 * The following code can be used to dump the queue into a newly |
631 * allocated array of {@code String}: |
628 * allocated array of {@code String}: |
632 * |
629 * |
633 * <pre> |
630 * <pre> {@code String[] y = x.toArray(new String[0]);}</pre> |
634 * String[] y = x.toArray(new String[0]);</pre> |
|
635 * |
631 * |
636 * Note that {@code toArray(new Object[0])} is identical in function to |
632 * Note that {@code toArray(new Object[0])} is identical in function to |
637 * {@code toArray()}. |
633 * {@code toArray()}. |
638 * |
634 * |
639 * @param a the array into which the elements of the queue are to |
635 * @param a the array into which the elements of the queue are to |
775 * subsequent to construction. |
771 * subsequent to construction. |
776 * |
772 * |
777 * @return an iterator over the elements in this queue in proper sequence |
773 * @return an iterator over the elements in this queue in proper sequence |
778 */ |
774 */ |
779 public Iterator<E> iterator() { |
775 public Iterator<E> iterator() { |
780 return new Itr(); |
776 return new Itr(); |
781 } |
777 } |
782 |
778 |
783 private class Itr implements Iterator<E> { |
779 private class Itr implements Iterator<E> { |
784 /* |
780 /* |
785 * Basic weakly-consistent iterator. At all times hold the next |
781 * Basic weakly-consistent iterator. At all times hold the next |
858 } |
854 } |
859 } |
855 } |
860 } |
856 } |
861 |
857 |
862 /** |
858 /** |
863 * Save the state to a stream (that is, serialize it). |
859 * Saves this queue to a stream (that is, serializes it). |
864 * |
860 * |
865 * @serialData The capacity is emitted (int), followed by all of |
861 * @serialData The capacity is emitted (int), followed by all of |
866 * its elements (each an {@code Object}) in the proper order, |
862 * its elements (each an {@code Object}) in the proper order, |
867 * followed by a null |
863 * followed by a null |
868 * @param s the stream |
|
869 */ |
864 */ |
870 private void writeObject(java.io.ObjectOutputStream s) |
865 private void writeObject(java.io.ObjectOutputStream s) |
871 throws java.io.IOException { |
866 throws java.io.IOException { |
872 |
867 |
873 fullyLock(); |
868 fullyLock(); |
885 fullyUnlock(); |
880 fullyUnlock(); |
886 } |
881 } |
887 } |
882 } |
888 |
883 |
889 /** |
884 /** |
890 * Reconstitute this queue instance from a stream (that is, |
885 * Reconstitutes this queue from a stream (that is, deserializes it). |
891 * deserialize it). |
|
892 * |
|
893 * @param s the stream |
|
894 */ |
886 */ |
895 private void readObject(java.io.ObjectInputStream s) |
887 private void readObject(java.io.ObjectInputStream s) |
896 throws java.io.IOException, ClassNotFoundException { |
888 throws java.io.IOException, ClassNotFoundException { |
897 // Read in capacity, and any hidden stuff |
889 // Read in capacity, and any hidden stuff |
898 s.defaultReadObject(); |
890 s.defaultReadObject(); |