Search in sources :

Example 76 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project sling by apache.

the class TestViewStateManager method setup.

@Before
public void setup() throws Exception {
    mgr = new ViewStateManagerImpl(new ReentrantLock(), new ClusterSyncService() {

        @Override
        public void sync(BaseTopologyView view, Runnable callback) {
            callback.run();
        }

        @Override
        public void cancelSync() {
        // nothing to cancel, we're auto-run
        }
    });
    // I want randomness yes, but deterministic, for some methods at least
    defaultRandom = new Random(1234123412);
    final org.apache.log4j.Logger discoveryLogger = RootLogger.getLogger("org.apache.sling.discovery");
    logLevel = discoveryLogger.getLevel();
    discoveryLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) ClusterSyncService(org.apache.sling.discovery.commons.providers.spi.ClusterSyncService) Random(java.util.Random) BaseTopologyView(org.apache.sling.discovery.commons.providers.BaseTopologyView) Before(org.junit.Before)

Example 77 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project sling by apache.

the class TestViewStateManager method testClusterSyncService_noConcurrency.

@Test
public void testClusterSyncService_noConcurrency() throws Exception {
    final org.apache.log4j.Logger commonsLogger = RootLogger.getLogger("org.apache.sling.discovery.commons.providers");
    //        final org.apache.log4j.Level logLevel = commonsLogger.getLevel();
    // change here to DEBUG in case of issues with this test
    commonsLogger.setLevel(Level.INFO);
    final Semaphore serviceSemaphore = new Semaphore(0);
    final ReentrantLock lock = new ReentrantLock();
    final ClusterSyncServiceWithSemaphore cs = new ClusterSyncServiceWithSemaphore(lock, serviceSemaphore);
    mgr = new ViewStateManagerImpl(lock, cs);
    final DummyListener listener = new DummyListener();
    mgr.bind(listener);
    TestHelper.assertNoEvents(listener);
    mgr.handleActivated();
    TestHelper.assertNoEvents(listener);
    final String slingId1 = UUID.randomUUID().toString();
    final String slingId2 = UUID.randomUUID().toString();
    final String clusterId = UUID.randomUUID().toString();
    final DefaultClusterView cluster = new DefaultClusterView(clusterId);
    final DummyTopologyView view1 = new DummyTopologyView().addInstance(slingId1, cluster, true, true).addInstance(slingId2, cluster, false, false);
    async(new Runnable() {

        @Override
        public void run() {
            mgr.handleNewView(view1);
        }
    });
    Thread.sleep(1000);
    TestHelper.assertNoEvents(listener);
    serviceSemaphore.release(1);
    Thread.sleep(1000);
    assertEvents(listener, EventHelper.newInitEvent(view1));
    final DummyTopologyView view2 = view1.clone();
    mgr.handleChanging();
    assertEvents(listener, EventHelper.newChangingEvent(view1));
    view2.removeInstance(slingId2);
    async(new Runnable() {

        @Override
        public void run() {
            mgr.handleNewView(view2);
        }
    });
    logger.debug("run: waiting for 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting no events");
    TestHelper.assertNoEvents(listener);
    logger.debug("run: releasing consistencyService");
    serviceSemaphore.release(1);
    logger.debug("run: waiting 1sec");
    Thread.sleep(1000);
    logger.debug("run: asserting 1 event");
    assertEvents(listener, EventHelper.newChangedEvent(view1, view2));
    // back to default
    commonsLogger.setLevel(Level.INFO);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) DummyTopologyView(org.apache.sling.discovery.commons.providers.DummyTopologyView) Semaphore(java.util.concurrent.Semaphore) DefaultClusterView(org.apache.sling.discovery.commons.providers.DefaultClusterView) Test(org.junit.Test)

Example 78 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class ArrayBlockingQueue method toArray.

/**
     * Returns an array containing all of the elements in this queue, in
     * proper sequence.
     *
     * <p>The returned array will be "safe" in that no references to it are
     * maintained by this queue.  (In other words, this method must allocate
     * a new array).  The caller is thus free to modify the returned array.
     *
     * <p>This method acts as bridge between array-based and collection-based
     * APIs.
     *
     * @return an array containing all of the elements in this queue
     */
public Object[] toArray() {
    final Object[] items = this.items;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        final int count = this.count;
        Object[] a = new Object[count];
        int n = items.length - takeIndex;
        if (count <= n) {
            System.arraycopy(items, takeIndex, a, 0, count);
        } else {
            System.arraycopy(items, takeIndex, a, 0, n);
            System.arraycopy(items, 0, a, n, count - n);
        }
        return a;
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 79 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class ArrayBlockingQueue method clear.

/**
     * Atomically removes all of the elements from this queue.
     * The queue will be empty after this call returns.
     */
public void clear() {
    final Object[] items = this.items;
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        int k = count;
        if (k > 0) {
            final int putIndex = this.putIndex;
            int i = takeIndex;
            do {
                items[i] = null;
            } while ((i = inc(i)) != putIndex);
            takeIndex = putIndex;
            count = 0;
            if (itrs != null)
                itrs.queueIsEmpty();
            for (; k > 0 && lock.hasWaiters(notFull); k--) notFull.signal();
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Example 80 with ReentrantLock

use of java.util.concurrent.locks.ReentrantLock in project robovm by robovm.

the class ArrayBlockingQueue method offer.

/**
     * Inserts the specified element at the tail of this queue if it is
     * possible to do so immediately without exceeding the queue's capacity,
     * returning {@code true} upon success and {@code false} if this queue
     * is full.  This method is generally preferable to method {@link #add},
     * which can fail to insert an element only by throwing an exception.
     *
     * @throws NullPointerException if the specified element is null
     */
public boolean offer(E e) {
    checkNotNull(e);
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        if (count == items.length)
            return false;
        else {
            enqueue(e);
            return true;
        }
    } finally {
        lock.unlock();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock)

Aggregations

ReentrantLock (java.util.concurrent.locks.ReentrantLock)1524 Lock (java.util.concurrent.locks.Lock)125 Test (org.junit.Test)78 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)74 Condition (java.util.concurrent.locks.Condition)57 ArrayList (java.util.ArrayList)45 IOException (java.io.IOException)42 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 File (java.io.File)22 TimeoutException (java.util.concurrent.TimeoutException)19 Before (org.junit.Before)18 AtomicReference (java.util.concurrent.atomic.AtomicReference)17 HashMap (java.util.HashMap)16 CountDownLatch (java.util.concurrent.CountDownLatch)16 ExecutionException (java.util.concurrent.ExecutionException)15 List (java.util.List)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 Map (java.util.Map)11 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)11 Pair (android.util.Pair)10