Search in sources :

Example 31 with AtomicReferenceArray

use of java.util.concurrent.atomic.AtomicReferenceArray in project lucene-solr by apache.

the class TestWeakIdentityMap method testConcurrentHashMap.

public void testConcurrentHashMap() throws Exception {
    // don't make threadCount and keyCount random, otherwise easily OOMs or fails otherwise:
    final int threadCount = 8, keyCount = 1024;
    final ExecutorService exec = Executors.newFixedThreadPool(threadCount, new NamedThreadFactory("testConcurrentHashMap"));
    final WeakIdentityMap<Object, Integer> map = WeakIdentityMap.newConcurrentHashMap(random().nextBoolean());
    // we keep strong references to the keys,
    // so WeakIdentityMap will not forget about them:
    final AtomicReferenceArray<Object> keys = new AtomicReferenceArray<>(keyCount);
    for (int j = 0; j < keyCount; j++) {
        keys.set(j, new Object());
    }
    try {
        for (int t = 0; t < threadCount; t++) {
            final Random rnd = new Random(random().nextLong());
            exec.execute(new Runnable() {

                @Override
                public void run() {
                    final int count = atLeast(rnd, 10000);
                    for (int i = 0; i < count; i++) {
                        final int j = rnd.nextInt(keyCount);
                        switch(rnd.nextInt(5)) {
                            case 0:
                                map.put(keys.get(j), Integer.valueOf(j));
                                break;
                            case 1:
                                final Integer v = map.get(keys.get(j));
                                if (v != null) {
                                    assertEquals(j, v.intValue());
                                }
                                break;
                            case 2:
                                map.remove(keys.get(j));
                                break;
                            case 3:
                                // renew key, the old one will be GCed at some time:
                                keys.set(j, new Object());
                                break;
                            case 4:
                                // check iterator still working
                                for (Iterator<Object> it = map.keyIterator(); it.hasNext(); ) {
                                    assertNotNull(it.next());
                                }
                                break;
                            default:
                                fail("Should not get here.");
                        }
                    }
                }
            });
        }
    } finally {
        exec.shutdown();
        while (!exec.awaitTermination(1000L, TimeUnit.MILLISECONDS)) ;
    }
    // clear strong refs
    for (int j = 0; j < keyCount; j++) {
        keys.set(j, null);
    }
    // check that GC does not cause problems in reap() method:
    int size = map.size();
    for (int i = 0; size > 0 && i < 10; i++) try {
        System.runFinalization();
        System.gc();
        int newSize = map.size();
        assertTrue("previousSize(" + size + ")>=newSize(" + newSize + ")", size >= newSize);
        size = newSize;
        Thread.sleep(100L);
        int c = 0;
        for (Iterator<Object> it = map.keyIterator(); it.hasNext(); ) {
            assertNotNull(it.next());
            c++;
        }
        newSize = map.size();
        assertTrue("previousSize(" + size + ")>=iteratorSize(" + c + ")", size >= c);
        assertTrue("iteratorSize(" + c + ")>=newSize(" + newSize + ")", c >= newSize);
        size = newSize;
    } catch (InterruptedException ie) {
    }
}
Also used : Random(java.util.Random) AtomicReferenceArray(java.util.concurrent.atomic.AtomicReferenceArray) ExecutorService(java.util.concurrent.ExecutorService) Iterator(java.util.Iterator)

Aggregations

AtomicReferenceArray (java.util.concurrent.atomic.AtomicReferenceArray)31 CountDownLatch (java.util.concurrent.CountDownLatch)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 RubyObject (org.jruby.RubyObject)6 IRubyObject (org.jruby.runtime.builtin.IRubyObject)6 Config (com.hazelcast.config.Config)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 Random (java.util.Random)4 Test (org.junit.Test)4 Thread.currentThread (java.lang.Thread.currentThread)3 ExecutorService (java.util.concurrent.ExecutorService)3 JoinConfig (com.hazelcast.config.JoinConfig)2 MulticastConfig (com.hazelcast.config.MulticastConfig)2 NetworkConfig (com.hazelcast.config.NetworkConfig)2 TcpIpConfig (com.hazelcast.config.TcpIpConfig)2 IMap (com.hazelcast.core.IMap)2 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 ArrayList (java.util.ArrayList)2