Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add BarrierEvent #60

Merged
merged 8 commits into from
Jan 11, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,43 @@ public void testSignalAllByMultiThreadWithSignalLast()
Assert.assertEquals(0, waitThreadInterruptedCount.get());
Assert.assertEquals(0, signalThreadInterruptedCount.get());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add testSignalAllByMultiThreadWithSignalAwaitConcurrent for signalAll and await at the same time

}

@Test
public void testSignalAllByMultiThreadWithSignalAwaitConcurrent()
throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
AtomicInteger eventCount = new AtomicInteger(0);
AtomicInteger waitThreadInterruptedCount = new AtomicInteger(0);
AtomicInteger signalThreadInterruptedCount = new AtomicInteger(0);
int waitThreadNum = 10;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define const var

ExecutorService executorService =
Executors.newFixedThreadPool(waitThreadNum + 1);
CountDownLatch syncLatch = new CountDownLatch(1);
for (int i = 0; i < waitThreadNum; i++) {
executorService.submit(() -> {
try {
syncLatch.await();
barrierEvent.await();
eventCount.incrementAndGet();
} catch (InterruptedException e) {
waitThreadInterruptedCount.incrementAndGet();
}
});
}

executorService.submit(() -> {
try {
syncLatch.await();
} catch (InterruptedException e) {
signalThreadInterruptedCount.incrementAndGet();
}
barrierEvent.signalAll();
});
syncLatch.countDown();
executorService.shutdown();
executorService.awaitTermination(1L, TimeUnit.SECONDS);
Assert.assertEquals(waitThreadNum, eventCount.get());
Assert.assertEquals(0, waitThreadInterruptedCount.get());
Assert.assertEquals(0, signalThreadInterruptedCount.get());
}
}