Created
February 8, 2018 17:23
-
-
Save nick318/fd41d239dd02222f1ac851db680aa3a9 to your computer and use it in GitHub Desktop.
hasNext method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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