Search in sources :

Example 6 with ConcurrentLinkedHashMap

use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.

the class GridConcurrentLinkedHashMapMultiThreadedSelfTest method testPutGetRemove.

/**
     * Test multithreaded put, get, remove operations in concurrent linked hash map.
     *
     *
     * @param clear {@code true} if test should expect clear map at the end.
     * @throws Exception If failed.
     */
private void testPutGetRemove(final boolean clear) throws Exception {
    info(">>> Test grid concurrent linked hash map iterator...");
    final ConcurrentLinkedHashMap<Integer, String> linkedMap = new ConcurrentLinkedHashMap<>();
    Collection<Integer> original = new HashSet<>();
    final int keyCnt = 10000;
    if (!clear)
        for (int i = 0; i < keyCnt; i++) {
            linkedMap.put(i, "value" + i);
            original.add(i);
        }
    long start = System.currentTimeMillis();
    // Updater threads.
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            Random rnd = new Random();
            int iterCnt = 100000;
            for (int i = 0; i < iterCnt; i++) {
                int key = rnd.nextInt(keyCnt);
                if (clear) {
                    linkedMap.put(key, "value" + key);
                    linkedMap.get(key);
                    linkedMap.remove(key);
                } else {
                    linkedMap.get(key);
                    linkedMap.remove(key);
                    linkedMap.put(key, "value" + key);
                }
            }
            return null;
        }
    }, 10, "updater");
    fut.get();
    Set<Integer> keys = linkedMap.keySet();
    if (clear)
        assertTrue("Keys must not be in map " + keys, keys.isEmpty());
    else {
        original.removeAll(keys);
        assertTrue("Keys must be in map: " + original, original.isEmpty());
    }
    info(">>> put get remove test complete [duration = " + (System.currentTimeMillis() - start) + ']');
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) Random(java.util.Random) Nullable(org.jetbrains.annotations.Nullable) HashSet(java.util.HashSet)

Example 7 with ConcurrentLinkedHashMap

use of org.jsr166.ConcurrentLinkedHashMap in project ignite by apache.

the class GridConcurrentLinkedHashMapSelfTest method testEvictionInsert.

/**
     *
     */
public void testEvictionInsert() {
    final int mapSize = 1000;
    Map<Integer, String> tst = new ConcurrentLinkedHashMap<>(10, 0.75f, 1, 1000);
    Map<Integer, String> map = new LinkedHashMap<Integer, String>(10, 0.75f, false) {

        @Override
        protected boolean removeEldestEntry(Map.Entry<Integer, String> eldest) {
            return size() > mapSize;
        }
    };
    for (int i = 0; i < mapSize; i++) {
        tst.put(i, "value" + i);
        map.put(i, "value" + i);
    }
    Random rnd1 = new Random();
    int iterCnt = 100000;
    int keyCnt = 10000;
    for (int i = 0; i < iterCnt; i++) {
        int key = rnd1.nextInt(keyCnt);
        tst.put(key, "value" + key);
        map.put(key, "value" + key);
    }
    Iterator<Map.Entry<Integer, String>> tstIt = tst.entrySet().iterator();
    for (Map.Entry<Integer, String> entry : map.entrySet()) {
        assertTrue("No enough elements in key set", tstIt.hasNext());
        Map.Entry<Integer, String> tstEntry = tstIt.next();
        assertEquals("Key mismatch", tstEntry.getKey(), entry.getKey());
        assertEquals("Value mismatch", tstEntry.getValue(), entry.getValue());
    }
    assertEquals("Invalid map size", mapSize, tst.size());
}
Also used : ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Random(java.util.Random) LinkedHashMap(java.util.LinkedHashMap) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ConcurrentLinkedHashMap(org.jsr166.ConcurrentLinkedHashMap)

Aggregations

ConcurrentLinkedHashMap (org.jsr166.ConcurrentLinkedHashMap)7 Random (java.util.Random)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 HashSet (java.util.HashSet)3 Nullable (org.jetbrains.annotations.Nullable)3 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 IgniteKernal (org.apache.ignite.internal.IgniteKernal)1 GridClientCompute (org.apache.ignite.internal.client.GridClientCompute)1 GridCacheTestStore (org.apache.ignite.internal.processors.cache.GridCacheTestStore)1 GridRestCommandHandler (org.apache.ignite.internal.processors.rest.handlers.GridRestCommandHandler)1 GridTaskCommandHandler (org.apache.ignite.internal.processors.rest.handlers.task.GridTaskCommandHandler)1 P1 (org.apache.ignite.internal.util.typedef.P1)1