Search in sources :

Example 11 with BarrierEvent

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);
}
Also used : BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Example 12 with BarrierEvent

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());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) BarrierEvent(com.baidu.hugegraph.concurrent.BarrierEvent) Test(org.junit.Test)

Aggregations

BarrierEvent (com.baidu.hugegraph.concurrent.BarrierEvent)12 Test (org.junit.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 ExecutorService (java.util.concurrent.ExecutorService)5 ComputerException (com.baidu.hugegraph.computer.core.common.exception.ComputerException)2 E (com.baidu.hugegraph.util.E)2 Log (com.baidu.hugegraph.util.Log)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ByteSequence (io.etcd.jetcd.ByteSequence)2 Client (io.etcd.jetcd.Client)2 KV (io.etcd.jetcd.KV)2 KeyValue (io.etcd.jetcd.KeyValue)2 Watch (io.etcd.jetcd.Watch)2 DeleteResponse (io.etcd.jetcd.kv.DeleteResponse)2 GetResponse (io.etcd.jetcd.kv.GetResponse)2 DeleteOption (io.etcd.jetcd.options.DeleteOption)2 GetOption (io.etcd.jetcd.options.GetOption)2 SortOrder (io.etcd.jetcd.options.GetOption.SortOrder)2 WatchOption (io.etcd.jetcd.options.WatchOption)2