40 import java.lang.invoke.VarHandle; |
40 import java.lang.invoke.VarHandle; |
41 import java.util.AbstractQueue; |
41 import java.util.AbstractQueue; |
42 import java.util.Collection; |
42 import java.util.Collection; |
43 import java.util.Collections; |
43 import java.util.Collections; |
44 import java.util.Iterator; |
44 import java.util.Iterator; |
|
45 import java.util.Objects; |
45 import java.util.Spliterator; |
46 import java.util.Spliterator; |
46 import java.util.Spliterators; |
47 import java.util.Spliterators; |
47 import java.util.concurrent.locks.LockSupport; |
48 import java.util.concurrent.locks.LockSupport; |
48 import java.util.concurrent.locks.ReentrantLock; |
49 import java.util.concurrent.locks.ReentrantLock; |
49 |
50 |
73 * <p>This class supports an optional fairness policy for ordering |
74 * <p>This class supports an optional fairness policy for ordering |
74 * waiting producer and consumer threads. By default, this ordering |
75 * waiting producer and consumer threads. By default, this ordering |
75 * is not guaranteed. However, a queue constructed with fairness set |
76 * is not guaranteed. However, a queue constructed with fairness set |
76 * to {@code true} grants threads access in FIFO order. |
77 * to {@code true} grants threads access in FIFO order. |
77 * |
78 * |
78 * <p>This class and its iterator implement all of the |
79 * <p>This class and its iterator implement all of the <em>optional</em> |
79 * <em>optional</em> methods of the {@link Collection} and {@link |
80 * methods of the {@link Collection} and {@link Iterator} interfaces. |
80 * Iterator} interfaces. |
|
81 * |
81 * |
82 * <p>This class is a member of the |
82 * <p>This class is a member of the |
83 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
83 * <a href="{@docRoot}/../technotes/guides/collections/index.html"> |
84 * Java Collections Framework</a>. |
84 * Java Collections Framework</a>. |
85 * |
85 * |
1110 * @throws ClassCastException {@inheritDoc} |
1110 * @throws ClassCastException {@inheritDoc} |
1111 * @throws NullPointerException {@inheritDoc} |
1111 * @throws NullPointerException {@inheritDoc} |
1112 * @throws IllegalArgumentException {@inheritDoc} |
1112 * @throws IllegalArgumentException {@inheritDoc} |
1113 */ |
1113 */ |
1114 public int drainTo(Collection<? super E> c) { |
1114 public int drainTo(Collection<? super E> c) { |
1115 if (c == null) |
1115 Objects.requireNonNull(c); |
1116 throw new NullPointerException(); |
|
1117 if (c == this) |
1116 if (c == this) |
1118 throw new IllegalArgumentException(); |
1117 throw new IllegalArgumentException(); |
1119 int n = 0; |
1118 int n = 0; |
1120 for (E e; (e = poll()) != null;) { |
1119 for (E e; (e = poll()) != null; n++) |
1121 c.add(e); |
1120 c.add(e); |
1122 ++n; |
|
1123 } |
|
1124 return n; |
1121 return n; |
1125 } |
1122 } |
1126 |
1123 |
1127 /** |
1124 /** |
1128 * @throws UnsupportedOperationException {@inheritDoc} |
1125 * @throws UnsupportedOperationException {@inheritDoc} |
1129 * @throws ClassCastException {@inheritDoc} |
1126 * @throws ClassCastException {@inheritDoc} |
1130 * @throws NullPointerException {@inheritDoc} |
1127 * @throws NullPointerException {@inheritDoc} |
1131 * @throws IllegalArgumentException {@inheritDoc} |
1128 * @throws IllegalArgumentException {@inheritDoc} |
1132 */ |
1129 */ |
1133 public int drainTo(Collection<? super E> c, int maxElements) { |
1130 public int drainTo(Collection<? super E> c, int maxElements) { |
1134 if (c == null) |
1131 Objects.requireNonNull(c); |
1135 throw new NullPointerException(); |
|
1136 if (c == this) |
1132 if (c == this) |
1137 throw new IllegalArgumentException(); |
1133 throw new IllegalArgumentException(); |
1138 int n = 0; |
1134 int n = 0; |
1139 for (E e; n < maxElements && (e = poll()) != null;) { |
1135 for (E e; n < maxElements && (e = poll()) != null; n++) |
1140 c.add(e); |
1136 c.add(e); |
1141 ++n; |
|
1142 } |
|
1143 return n; |
1137 return n; |
1144 } |
1138 } |
1145 |
1139 |
1146 /* |
1140 /* |
1147 * To cope with serialization strategy in the 1.5 version of |
1141 * To cope with serialization strategy in the 1.5 version of |