Search in sources :

Example 1 with ConcurrentOpenHashSet

use of com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashSet in project pulsar by yahoo.

the class ConcurrentOpenHashSetTest method concurrentInsertionsAndReads.

@Test
public void concurrentInsertionsAndReads() throws Throwable {
    ConcurrentOpenHashSet<Long> map = new ConcurrentOpenHashSet<>();
    ExecutorService executor = Executors.newCachedThreadPool();
    final int nThreads = 16;
    final int N = 100_000;
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < nThreads; i++) {
        final int threadIdx = i;
        futures.add(executor.submit(() -> {
            Random random = new Random();
            for (int j = 0; j < N; j++) {
                long key = random.nextLong();
                // Ensure keys are unique
                key -= key % (threadIdx + 1);
                map.add(key);
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    assertEquals(map.size(), N * nThreads);
    executor.shutdown();
}
Also used : Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ConcurrentOpenHashSet(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashSet) Test(org.testng.annotations.Test)

Example 2 with ConcurrentOpenHashSet

use of com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashSet in project pulsar by yahoo.

the class ConcurrentOpenHashSetTest method concurrentInsertions.

@Test
public void concurrentInsertions() throws Throwable {
    ConcurrentOpenHashSet<Long> set = new ConcurrentOpenHashSet<>();
    ExecutorService executor = Executors.newCachedThreadPool();
    final int nThreads = 16;
    final int N = 100_000;
    List<Future<?>> futures = new ArrayList<>();
    for (int i = 0; i < nThreads; i++) {
        final int threadIdx = i;
        futures.add(executor.submit(() -> {
            Random random = new Random();
            for (int j = 0; j < N; j++) {
                long key = random.nextLong();
                // Ensure keys are unique
                key -= key % (threadIdx + 1);
                set.add(key);
            }
        }));
    }
    for (Future<?> future : futures) {
        future.get();
    }
    assertEquals(set.size(), N * nThreads);
    executor.shutdown();
}
Also used : Random(java.util.Random) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) ConcurrentOpenHashSet(com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashSet) Test(org.testng.annotations.Test)

Aggregations

ConcurrentOpenHashSet (com.yahoo.pulsar.common.util.collections.ConcurrentOpenHashSet)2 ArrayList (java.util.ArrayList)2 Random (java.util.Random)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 Test (org.testng.annotations.Test)2