Skip to content

Commit ca7bb73

Browse files
committed
Update Pipe.
1 parent 0efe4d7 commit ca7bb73

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

  • kilo-client/src/main/java/org/httprpc/kilo/util/concurrent

kilo-client/src/main/java/org/httprpc/kilo/util/concurrent/Pipe.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
* The element type.
2929
*/
3030
public class Pipe<E> implements Iterable<E> {
31-
private BlockingQueue<Object> queue;
31+
private volatile BlockingQueue<Object> queue;
32+
3233
private int timeout;
3334

3435
private static final Object TERMINATOR = new Object();
@@ -81,6 +82,10 @@ public void submit(Iterable<? extends E> elements) {
8182
throw new IllegalArgumentException();
8283
}
8384

85+
if (queue == null) {
86+
throw new IllegalStateException();
87+
}
88+
8489
for (var element : elements) {
8590
submit(element);
8691
}
@@ -112,6 +117,10 @@ public Iterator<E> iterator() {
112117
@Override
113118
@SuppressWarnings("unchecked")
114119
public boolean hasNext() {
120+
if (queue == null) {
121+
return false;
122+
}
123+
115124
if (next == null) {
116125
Object value;
117126
try {
@@ -130,6 +139,8 @@ public boolean hasNext() {
130139

131140
if (value != TERMINATOR) {
132141
next = (E)value;
142+
} else {
143+
queue = null;
133144
}
134145
}
135146

0 commit comments

Comments
 (0)