Skip to content

Instantly share code, notes, and snippets.

@nick318
Created February 8, 2018 17:23
Show Gist options
  • Save nick318/fd41d239dd02222f1ac851db680aa3a9 to your computer and use it in GitHub Desktop.
Save nick318/fd41d239dd02222f1ac851db680aa3a9 to your computer and use it in GitHub Desktop.
hasNext method
@Test
public void correctValuesForConcurrentHasNext() throws ExecutionException, InterruptedException {
int exceptionCount = 0;
for (int i = 0; i <5000; i++) {
Iterator<Integer> syncIterator = new SyncIterator<Integer>(
Stream.iterate(0, j -> j + 1).limit(200)
.collect(toList())
.iterator());
CountDownLatch startSignal = new CountDownLatch(1);
ExecutorService executors = Executors.newFixedThreadPool(2);
Future<Boolean> hasNext = executors.submit(() -> {
startSignal.await();
while (syncIterator.hasNext()) {
syncIterator.next();
} return true;
});
Future<Boolean> hasNext2 = executors.submit(() -> {
startSignal.await();
while (syncIterator.hasNext()) {
syncIterator.next();
} return true;
});
startSignal.countDown();
try {
MatcherAssert.assertThat(
"Error",
hasNext.get(),
Matchers.equalTo(hasNext2.get()));
} catch (ExecutionException e) {
exceptionCount++;
}
}
System.out.println(exceptionCount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment