Search in sources :

Example 1 with RowLock

use of com.baidu.hugegraph.concurrent.RowLock in project hugegraph-common by hugegraph.

the class RowLockTest method testRowLockWithMultiThreadsWithRandomKey.

@Test
public void testRowLockWithMultiThreadsWithRandomKey() {
    RowLock<Integer> lock = new RowLock<>();
    Set<String> names = new HashSet<>(THREADS_NUM);
    Assert.assertEquals(0, names.size());
    runWithThreads(THREADS_NUM, () -> {
        List<Integer> keys = new ArrayList<>(5);
        Random random = new Random();
        for (int i = 0; i < 5; i++) {
            keys.add(random.nextInt(THREADS_NUM));
        }
        lock.lockAll(new HashSet<>(keys));
        names.add(Thread.currentThread().getName());
        lock.unlockAll(new HashSet<>(keys));
    });
    Assert.assertEquals(THREADS_NUM, names.size());
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) RowLock(com.baidu.hugegraph.concurrent.RowLock) HashSet(java.util.HashSet) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 2 with RowLock

use of com.baidu.hugegraph.concurrent.RowLock in project hugegraph-common by hugegraph.

the class RowLockTest method testRowLockWithMultiThreads.

@Test
public void testRowLockWithMultiThreads() {
    RowLock<Integer> lock = new RowLock<>();
    Set<String> names = new HashSet<>(THREADS_NUM);
    List<Integer> keys = new ArrayList<>(5);
    Random random = new Random();
    for (int i = 0; i < 5; i++) {
        keys.add(random.nextInt(THREADS_NUM));
    }
    Assert.assertEquals(0, names.size());
    runWithThreads(THREADS_NUM, () -> {
        lock.lockAll(new HashSet<>(keys));
        names.add(Thread.currentThread().getName());
        lock.unlockAll(new HashSet<>(keys));
    });
    Assert.assertEquals(THREADS_NUM, names.size());
}
Also used : Random(java.util.Random) ArrayList(java.util.ArrayList) RowLock(com.baidu.hugegraph.concurrent.RowLock) HashSet(java.util.HashSet) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

RowLock (com.baidu.hugegraph.concurrent.RowLock)2 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Random (java.util.Random)2 Test (org.junit.Test)2