Search in sources :

Example 96 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project tomee by apache.

the class PoolTest method testFlush.

public void testFlush() throws Exception {
    System.out.println("PoolTest.testFlush");
    final int min = 4;
    final int max = 9;
    final int sweepInterval = 200;
    final List<Bean> discarded = new CopyOnWriteArrayList<Bean>();
    final CountDownLatch discard = new CountDownLatch(max);
    final CountDownLatch created = new CountDownLatch(min);
    final CountDownLatch createInstances = new CountDownLatch(1);
    final Pool.Builder builder = new Pool.Builder();
    builder.setMinSize(min);
    builder.setMaxSize(max);
    builder.setSweepInterval(new Duration(sweepInterval, TimeUnit.MILLISECONDS));
    builder.setSupplier(new Pool.Supplier<Bean>() {

        public void discard(final Bean bean, final Pool.Event reason) {
            bean.discard();
            discarded.add(bean);
            discard.countDown();
        }

        public Bean create() {
            try {
                createInstances.await();
            } catch (final InterruptedException e) {
                Thread.interrupted();
            }
            try {
                return new Bean();
            } finally {
                created.countDown();
            }
        }
    });
    final Pool pool = this.pool = builder.build().start();
    // Fill pool to max
    Bean.instances.set(0);
    for (int i = 0; i < max; i++) {
        assertTrue(pool.add(new Bean()));
    }
    {
        // Should have a full, non-null pool
        final List entries = drain(pool, 100);
        checkMin(min, entries);
        checkEntries(max, entries);
        push(pool, entries);
    }
    pool.flush();
    // Wait for the Evictor to come around and sweep out the pool
    await(discard, 10, TimeUnit.SECONDS);
    // All instances should have been removed
    assertEquals(max, discarded.size());
    for (final Bean bean : discarded) {
        final long flushTime = bean.discarded - bean.accessed;
        // It shouldn't be too much more either
        assertTrue("flush took too long", flushTime < (sweepInterval * 2));
    }
    // Minimum instances are still being created
    // The rest of the pool should be empty (null)
    {
        final List entries = drain(pool, 100);
        // Should have "non-min" number of entries
        checkMax(max - min, entries);
        // Nothing should be a "min" item as those
        // are still being created
        checkMin(0, entries);
        // And as the pool was just drained all the
        // entries we get should be null
        checkNull(entries);
        push(pool, entries);
    }
    Bean.instances.set(0);
    // Try and trick the pool into adding more "min" items
    // Fill the pool as much as we can -- should only let us
    // fill to the max factoring in the "min" instances that
    // are currently being created
    {
        final List entries = drain(pool, 100);
        // Should reject the instance we try to add
        assertFalse(pool.add(new Bean()));
        // Empty the pool
        discard(pool, entries);
        // Now count how many instances it lets us add
        Bean.instances.set(0);
        while (pool.add(new Bean())) ;
        // As the "min" instances are still being created
        // it should only let us fill max - min, then + 1
        // to account for the instance that gets rejected
        // and terminates the while loop
        final int expected = max - min + 1;
        assertEquals(expected, Bean.instances.getAndSet(0));
    }
    // Ok, let the "min" instance creation threads continue
    createInstances.countDown();
    // Wait for the "min" instance creation to complete
    assertTrue(created.await(sweepInterval * 10, TimeUnit.MILLISECONDS));
    {
        // Pool should be full again
        final List entries = drain(pool, 100);
        checkMin(min, entries);
        checkEntries(max, entries);
        push(pool, entries);
    }
//  -- DONE --
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 97 with CopyOnWriteArrayList

use of java.util.concurrent.CopyOnWriteArrayList in project tomee by apache.

the class StatelessContainer method retrieveAroundInvokes.

private List<Method> retrieveAroundInvokes(final Class<?> interceptorClass) {
    final List<Method> cached = this.interceptorCache.get(interceptorClass);
    if (cached != null) {
        return cached;
    }
    final ClassFinder finder = new ClassFinder(interceptorClass);
    List<Method> annotated = finder.findAnnotatedMethods(AroundInvoke.class);
    if (StatelessContainer.class.getClassLoader() == interceptorClass.getClassLoader()) {
        // use cache only for server classes
        final List<Method> value = new CopyOnWriteArrayList<Method>(annotated);
        // ensure it to be thread safe
        annotated = this.interceptorCache.putIfAbsent(interceptorClass, annotated);
        if (annotated == null) {
            annotated = value;
        }
    }
    return annotated;
}
Also used : ClassFinder(org.apache.xbean.finder.ClassFinder) Method(java.lang.reflect.Method) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)97 CountDownLatch (java.util.concurrent.CountDownLatch)38 IOException (java.io.IOException)23 ArrayList (java.util.ArrayList)23 List (java.util.List)23 Test (org.junit.Test)20 ExecutionException (java.util.concurrent.ExecutionException)15 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)13 CyclicBarrier (java.util.concurrent.CyclicBarrier)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)10 MockTransportService (org.elasticsearch.test.transport.MockTransportService)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9 HashMap (java.util.HashMap)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 HashSet (java.util.HashSet)6 Map (java.util.Map)6 TimeUnit (java.util.concurrent.TimeUnit)6 Settings (org.elasticsearch.common.settings.Settings)6 Config (com.hazelcast.config.Config)5