use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.
the class BarrierEventTest method testSignal.
@Test
public void testSignal() throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
boolean signaled = barrierEvent.await(1L);
Assert.assertFalse(signaled);
barrierEvent.signal();
signaled = barrierEvent.await(1L);
Assert.assertTrue(signaled);
}
use of com.baidu.hugegraph.concurrent.BarrierEvent in project hugegraph-common by hugegraph.
the class BarrierEventTest method testSignalAllByMultiThreadWithSignalFirst.
@Test
public void testSignalAllByMultiThreadWithSignalFirst() throws InterruptedException {
BarrierEvent barrierEvent = new BarrierEvent();
AtomicInteger eventCount = new AtomicInteger(0);
AtomicInteger waitThreadInterruptedCount = new AtomicInteger(0);
ExecutorService executorService = Executors.newFixedThreadPool(WAIT_THREADS_COUNT + 1);
CountDownLatch waitLatch = new CountDownLatch(WAIT_THREADS_COUNT);
CountDownLatch signalLatch = new CountDownLatch(1);
for (int i = 0; i < WAIT_THREADS_COUNT; i++) {
executorService.submit(() -> {
try {
signalLatch.await();
waitLatch.countDown();
barrierEvent.await();
eventCount.incrementAndGet();
} catch (InterruptedException e) {
waitThreadInterruptedCount.incrementAndGet();
}
});
}
executorService.submit(() -> {
barrierEvent.signalAll();
signalLatch.countDown();
});
executorService.shutdown();
executorService.awaitTermination(1L, TimeUnit.SECONDS);
Assert.assertEquals(10, eventCount.get());
Assert.assertEquals(0, waitThreadInterruptedCount.get());
}
Aggregations