Skip to content

Commit

Permalink
CancellableCollectSpliterator: race fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
amaembo committed Sep 11, 2015
1 parent 276f100 commit 0b2786e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
Expand Up @@ -52,21 +52,23 @@ public boolean tryAdvance(Consumer<? super A> action) {
Spliterator<T> source = this.source;
if(source == null)
return false;
this.source = null;
acc = supplier.get();
if(cancelled == null) {
this.source = null;
// sequential mode
while(!cancelPredicate.test(acc) && source.tryAdvance(this)) {
// empty
}
} else {
do {
if(cancelPredicate.test(acc)) {
this.source = null;
if(isFinished())
cancelled.set(true);
break;
}
} while(!cancelled.get() && source.tryAdvance(this));
this.source = null;
}
action.accept(acc);
return true;
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/javax/util/streamex/MoreCollectorsTest.java
Expand Up @@ -153,6 +153,13 @@ public void testFirstLast() {
assertFalse(supplier.toString(), supplier.get().collect(MoreCollectors.last()).isPresent());
}
}

@Test
public void testHeadTailParallel() {
for(int i=0; i<100; i++) {
assertEquals("#"+i, Arrays.asList(0, 1), IntStreamEx.range(1000).boxed().parallel().collect(MoreCollectors.head(2)));
}
}

@Test
public void testHeadTail() {
Expand Down

0 comments on commit 0b2786e

Please sign in to comment.