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